vendredi 6 mars 2015

How to avoid error stack track

I am using expressjs' body-parser to parse the body of a request into json, but when I pass invalid json the body-parser middleware sends an error message with more information than necessary.


Here's how I use body parser:



// Use bodyParser to read data from body of Http requests
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json


Here's an example of the information passed back when I post with invalid json:



Error: invalid json
<br> &nbsp; &nbsp;at parse (/Users/.../node_modules/body-parser/lib/types/json.js:72:15)
<br> &nbsp; &nbsp;at /Users/.../node_modules/body-parser/lib/read.js:98:18
<br> &nbsp; &nbsp;at IncomingMessage.onEnd (/Users/.../node_modules/body-parser/node_modules/raw-body/index.js:136:7)
<br> &nbsp; &nbsp;at IncomingMessage.g (events.js:180:16)
<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:92:17)
<br> &nbsp; &nbsp;at _stream_readable.js:943:16
<br> &nbsp; &nbsp;at process._tickCallback (node.js:419:13)


Is there an option I can use to avoid these kinds of responses? I checked the documentation but didn't find anything useful.


Aucun commentaire:

Enregistrer un commentaire