express-body-parser-error-handler
Advanced tools
Comparing version 1.0.2 to 1.0.3
58
index.js
@@ -1,37 +0,35 @@ | ||
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', | ||
'encoding.unsupported', | ||
'entity.too.large' | ||
] | ||
return bodyParserCommonErrorsTypes.includes(error.type) | ||
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', | ||
'encoding.unsupported', | ||
'entity.too.large' | ||
] | ||
return bodyParserCommonErrorsTypes.includes(error.type) | ||
} | ||
function bodyParserErrorHandler( | ||
{ | ||
// eslint-disable-next-line | ||
function bodyParserErrorHandler ( | ||
{ | ||
// eslint-disable-next-line | ||
onError = (err, req, res) => { | ||
}, | ||
errorMessage = (err) => { | ||
return `Body Parser failed to parse request --> ${err.message}` | ||
} | ||
} = {}) { | ||
return (err, req, res, next) => { | ||
if (err && isBodyParserError(err)) { | ||
onError(err, req, res) | ||
res.status(err.status) | ||
res.send({message: errorMessage(err)}) | ||
} else if (err) { | ||
next(err) | ||
} else next() | ||
}, | ||
errorMessage = (err) => { | ||
return `Body Parser failed to parse request --> ${err.message}` | ||
} | ||
} = {}) { | ||
return (err, req, res, next) => { | ||
if (err && isBodyParserError(err)) { | ||
onError(err, req, res) | ||
res.status(err.status) | ||
res.send({ message: errorMessage(err) }) | ||
} else next(err) | ||
} | ||
} | ||
module.exports = bodyParserErrorHandler |
{ | ||
"name": "express-body-parser-error-handler", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "middleware to be set right after body parser in order to return 4xx error back to the client", | ||
@@ -41,7 +41,7 @@ "scripts": { | ||
"describe", | ||
"before", | ||
"beforeAll", | ||
"it", | ||
"after" | ||
"afterAll" | ||
] | ||
} | ||
} |
@@ -61,4 +61,4 @@ # express-body-parser-error-handler | ||
// body parser initilization | ||
app.use(urlencoded({extended: false, limit: defaultLimitSize})); | ||
app.use('/', json({limit: '250'})); | ||
app.use(urlencoded({extended: false, limit: '250kb'})); | ||
app.use('/', json({limit: '250kb'})); | ||
@@ -65,0 +65,0 @@ // body parser error handler |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6893
33