Fix to return a 400 instead of a 500 when using the withContent middleware and sending no content or invalid JSON in the request body.
Description
Wrapped the withContent middleware with a try... catch to capture any JSON parsing issues and return them as 400 Bad Request instead of 500 Internal Server Error. Added tests to check for the error, and that the router returns a 400 when no content or invalid JSON content is sent in the request body.
[x] Breaking change (fix that may cause userland code to not work as expected)
[x] If anyone relies upon the HTTP status code with the withContent middleware, this may cause an issue. Otherwise, this captures the invalid JSON input and returns a "more correct" HTTP status code.
Fix to return a
400
instead of a500
when using thewithContent
middleware and sending no content or invalid JSON in the request body.Description
Wrapped the
withContent
middleware with atry... catch
to capture any JSON parsing issues and return them as400 Bad Request
instead of500 Internal Server Error
. Added tests to check for the error, and that the router returns a400
when no content or invalid JSON content is sent in the request body.Related Issue
Link to the related issue: 216: 'withContent' returns a '500' when no body is sent
Type of Change
withContent
middleware, this may cause an issue. Otherwise, this captures the invalid JSON input and returns a "more correct" HTTP status code.