ngrunwald / ring-middleware-format

Ring middleware for parsing parameters and emitting responses in JSON or other formats
163 stars 49 forks source link

How to deal with invalid JSON? #25

Closed yayitswei closed 10 years ago

yayitswei commented 10 years ago

When given invalid json, wrap_format_params fails with an exception, com.fasterxml.jackson.core.JsonParseException: Unexpected character .... What's a good way to handle this exception and return a 400 status code?

oluci commented 10 years ago

Hi, did you find an answer for this problem? I have the same issue. Thanks

ngrunwald commented 10 years ago

Sorry for the delay. You can actually give an error handler to the middleware, and the 0.4.0 release from a few hours ago juste made it easier. Here is an exemple:

(wrap-restful-format handler :request-error-handler
   (fn [exception _ request]
      (cond
         (instance? com.fasterxml.jackson.core.JsonParseException exception)
         {:status 400 :body "Error parsing JSON body"}
         :else {:status 500 :body "Unknown error"})))

Does that answer your problem? If it does, I'll add something like this to the README...

oluci commented 10 years ago

Thanks for getting replying. This answers the problem.