interagent / pliny

An opinionated toolkit for writing excellent APIs in Ruby.
MIT License
801 stars 73 forks source link

Return 400s on unparseable JSON POST payloads #338

Closed kbarber closed 3 years ago

kbarber commented 3 years ago

When someone tries to parse bad request JSON, pliny right now will by default return a 500 via the web with an exception such as this logged:

MultiJson::ParseError: unexpected character (after foo.bar) at line 1, column 14 [parse.c:724]

Invalid JSON post bodies should instead return a 400, and the parse error somewhere in the body.

This PR wraps around the place we parse JSON and returns the appropriate pliny exception with a more helpful message attached to the response.

kbarber commented 3 years ago

@beanieboi this is what I was thinking in regards to those 500's we were getting. Much easier than wrapping our code in multiple parse error rescues :-). /cc @stevenharman.

kbarber commented 3 years ago

@stevenharman all fixed, thanks for the comments.