Closed jeffcharles closed 10 years ago
I have this problem as well, looks like issue #11 would allow us to add an on-error handler which would be a good solution.
To be clear, a 500 error message is only returned if you don't catch the exception. If you catch the exception, you can return whatever error message you like.
I guess it might be useful to have a default option to catch the exceptions and return a 400 message, though.
I think defaulting to a 400 would be best in the case of malformed json only. If we default to 400 for any exception it's misleading. Maybe it's best to just wrap the middleware in a try/catch and handle exceptions yourself as you say in #11.
I'm hoping to revive the discussion with the pull request I submitted. I'm trying to piece together the discussions across #11 and #7. I can't tell what was the resolution. It sounds to me that there is a favorable disposition towards catching malformed JSON and defaulting with a 400 status. Is this correct?
Fixed in 80b0e76f5d.
When posting a request with invalid JSON in the request body, a 500 status code is returned by the web server. I would've expected a 400 status code to be returned because the request could not be understood by the server due to malformed syntax (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1) rather than a 500 status code which indicates a server-side error.
From looking through ring-json's and Cheshire's source code, I can see that nothing seems to anticipate that malformed JSON could be presented for parsing. Sometimes third party clients have a mistake with the JSON they send to my web service and it would be nice to send them an appropriate response to indicate the error is on their end instead of mine.
Here's the request I sent (missing closing brace in the body is the malformed part):
Here's the stack trace on the server:
In an ideal world, I'd prefer to be able to specify a function/handler to call when malformed JSON is present in the request body so I can specify what the response should look like. Unfortunately, I'm new to Clojure and Ring so I'm not entirely sure where a logical place to specify that handler would be if that functionality were to be added.