-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
36 lines (33 loc) · 950 Bytes
/
index.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
function isBodyParserError(error) {
const bodyParserCommonErrorsTypes = [
"encoding.unsupported",
"entity.parse.failed",
"entity.verify.failed",
"request.aborted",
"request.size.invalid",
"stream.encoding.set",
"parameters.too.many",
"charset.unsupported",
"entity.too.large",
];
return bodyParserCommonErrorsTypes.includes(error.type);
}
function bodyParserErrorHandler({
// eslint-disable-next-line
onError = (err, req, res) => {},
// i think "errorMessage" function is useless now
errorMessage = (err) => {
return `Body Parser failed to parse request --> ${err.message}`;
},
} = {}) {
return (err, req, res, next) => {
if (err && isBodyParserError(err)) {
onError(err, req, res, next);
if (!res.headersSent) {
res.status(err.status);
res.send({ message: errorMessage(err) });
}
} else next(err);
};
}
module.exports = bodyParserErrorHandler;