I want to catch the error from the bodyParser() middleware when I send a json object and it is invalid because I want to send a custom response instead of a generic 400 error.
This is what I have and it works:
app.use (express.bodyParser ()); app.use (function (error, req, res, next){ //Catch bodyParser error if (error.message === "invalid json"){ sendError (res, myCustomErrorMessage); }else{ next (); } });
But this seems to me a very ugly approach because I’m comparing the error message which could change in future express versions. There’s any other way to catch bodyParser() errors?
EDIT:
This is the error when the request body has an invalid json:
{ stack: 'Error: invalid jsonn at Object.exports.error (<path>/node_modules/express/node_modules/connect/lib/utils.js:55:13)n at IncomingMessage.<anonymous> (<path>/node_modules/express/node_modules/connect/lib/middleware/json.js:74:71)n at IncomingMessage.EventEmitter.emit (events.js:92:17)n at _stream_readable.js:872:14n at process._tickDomainCallback (node.js:459:13)', arguments: undefined, type: undefined, message: 'invalid json', status: 400 }
Pretty printed stack:
Error: invalid json at Object.exports.error (<path>/node_modules/express/node_modules/connect/lib/utils.js:55:13) at IncomingMessage.<anonymous> (<path>/node_modules/express/node_modules/connect/lib/middleware/json.js:74:71) at IncomingMessage.EventEmitter.emit (events.js:92:17) at _stream_readable.js:872:14 at process._tickDomainCallback (node.js:459:13)
Advertisement
Answer
Ok, found it:
bodyParser() is a convenience function for json(), urlencoded() and multipart(). I just need to call to json(), catch the error and call to urlencoded() and multipart().
app.use (express.json ()); app.use (function (error, req, res, next){ //Catch json error sendError (res, myCustomErrorMessage); }); app.use (express.urlencoded ()); app.use (express.multipart ());