visionmedia/express — Issue #872: Cannot detect invalid JSON in bodyParser
Author: secoif
Created: 2011-10-29 00:36:02+00:00
State: open

I'm trying to return a non-500 error + message when someone POST's invalid JSON, but I can't seem to capture this error.

It appears that `JSON.parse` doesn't throw any exceptions as I would expect, and thus doesn't next() any errors because as far as it's concerned there are none. :/

The invalid JSON is only reported later when the data is passed to `methodOverride`:

```
TypeError: Cannot use 'in' operator to search for '_method' in {ThisIsNotValidJson
at String.IN (native)
at Object.methodOverride [as handle] (/Users/secoif/Projects/.../node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:27:31)
at next (/Users/secoif/Projects/.../node_modules/express/node_modules/connect/lib/http.js:201:15)
at Object.cookieParser [as handle] (/Users/secoif/Projects/.../node_modules/express/node_modules/connect/lib/middleware/cookieParser.js:44:5)
at next (/Users/secoif/Projects/.../node_modules/express/node_modules/connect/lib/http.js:201:15)
at next (/Users/secoif/Projects/.../node_modules/express/node_modules/connect/lib/http.js:203:9)
at IncomingMessage. (/Users/secoif/Projects/.../node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:81:9)
at IncomingMessage.emit (events.js:61:17)
at HTTPParser.onMessageComplete (http.js:133:23)
at Socket.ondata (http.js:1029:22)
```

Someone with the exact same issue here: http://stackoverflow.com/questions/7478917/catching-illegal-json-post-data-in-express