Reputation: 7944
I am creating an API in express.js where by the user posts JSON.
I've set the content type to application/json in the clients request. If I post valid JSON, all is well. However, posintg malformed JSON results in express throwing the following error...
string(370) "SyntaxError: Unexpected token } at Object.parse (native) at IncomingMessage. (/var/www/vhost/rest-v1/node/node_modules/express/node_modules/connect/lib/middleware/json.js:76:27) at IncomingMessage.EventEmitter.emit (events.js:92:17) at _stream_readable.js:910:16 at process._tickCallback (node.js:415:13)"
How can I catch this error so that I can respond with a more meaningful error message to the user?
Upvotes: 2
Views: 2506
Reputation: 34337
User Express's built in error handling middleware or override it with your own: http://expressjs.com/guide.html#error-handling
Upvotes: 1