-
Notifications
You must be signed in to change notification settings - Fork 4
/
handle-errors.js
37 lines (33 loc) · 741 Bytes
/
handle-errors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import {
HafasError,
HafasInvalidRequestError,
HafasNotFoundError,
} from 'hafas-client/lib/errors.js'
const createErrorHandler = (logger) => {
const handleErrors = (err, req, res, next) => {
logger.error(err)
if (res.headersSent) return next()
let msg = err.message, code = err.statusCode || null
if (err instanceof HafasError) {
msg = 'HAFAS error: ' + msg
if (err instanceof HafasInvalidRequestError) {
code = 400
} else if (err instanceof HafasNotFoundError) {
code = 404
} else {
code = 502
}
}
res.status(code || 500).json({
message: err.message || null,
...err,
request: undefined,
response: undefined,
})
next()
}
return handleErrors
}
export {
createErrorHandler,
}