forked from middyjs/middy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
28 lines (23 loc) 路 650 Bytes
/
Copy pathindex.js
File metadata and controls
28 lines (23 loc) 路 650 Bytes
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
module.exports = (opts) => {
const defaults = {
logger: console.error
}
const options = Object.assign({}, defaults, opts)
return ({
onError: (handler, next) => {
// if there are a `statusCode` and an `error` field
// this is a valid http error object
if (handler.error.statusCode && handler.error.message) {
if (typeof options.logger === 'function') {
options.logger(handler.error)
}
handler.response = {
statusCode: handler.error.statusCode,
body: handler.error.message
}
return next()
}
return next(handler.error)
}
})
}