kohesive / kovert

The invisible REST and web framework
MIT License
155 stars 10 forks source link

Kovert doesn't allow json body on 400 #34

Closed adambrown closed 8 years ago

adambrown commented 8 years ago

Kovert doesn't seem to allow returning an application/json body with a 400 error even though HTTP allows and actually strongly recommends this. Sometimes on an invalid request way more details are needed than just an http code and message.

Here is the relevant part of http spec:

10.4 Client Error 4xx

The 4xx class of status code is intended for cases in which the client seems to have erred. Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method. User agents SHOULD display any included entity to the user.

If the client is sending data, a server implementation using TCP SHOULD be careful to ensure that the client acknowledges receipt of the packet(s) containing the response, before the server closes the input connection. If the client continues sending data to the server after the close, the server's TCP stack will send a reset packet to the client, which may erase the client's unacknowledged input buffers before they can be read and interpreted by the HTTP application.

Note that all caps SHOULD include and entity containing an explanation of the error.

A good example of where this is needed and used effectively is card validation error codes with Stripe:

https://stripe.com/docs/api/java#errors

apatrida commented 8 years ago

You can throw an HttpErrorCodeWithBody which accepts the message (reason string), code (status code), and root cause exception same as HttpErrorCode class, but adds additional body parameter of type Any which can be a string (text/html) or an object that will be turned into JSON (application/json)

fixed in 0.13.0 to be released in the next few hours.