-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.js
33 lines (30 loc) · 820 Bytes
/
handlers.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
const Boom = require('@hapi/boom');
module.exports = (debug) => {
function formatBoomPayload(error) {
if (debug) {
error.reformat(true);
}
return {
...error.output.payload,
...(error.data ? { data: error.data } : {}),
...(debug && error.stack? { stack: error.stack } : {})
};
}
return {
error(err, req, res, next) {
if (err instanceof Error) {
if (!Boom.isBoom(err)) {
Boom.boomify(err);
}
res.status(err.output.statusCode).send(formatBoomPayload(err));
}
return next();
},
notFound(req, res, next) {
return req.route? next() : next(Boom.notFound());
},
methodNotAllowed(req, res, next) {
return next(Boom.methodNotAllowed(`${req.method} ${req.originalUrl || req.url}`));
}
};
};