-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ConstraintViolationException returned as HttpMediaTypeNotAcceptableException #23421
Comments
There was a fix in Spring Framework 5.1, see #20865 to ensure that error response are not impacted the produces condition mapping, which would have been an issue for this scenario, and I have confirmed that with Spring Framework only this works as expected returning the underlying error. In Spring Boot however, unhandled errors are rendered with a response body either as HTML or as JSON, and the "text/plain" matches neither. So technically Boot is correct in not rendering the content types it can render error content with aren't acceptable. The answer might be in some way of customizing Boot's error handling? Or perhaps the Boot team might consider some enhancement for this? Either way it is not something we can fix in the Spring Framework so I'm going to close this. /cc @bclozel |
Here are a few ideas:
Back to the first idea, I think the HTTP clients are fully in control here and could request something like I can't think of any other feature that would improve this in Spring Framework or Spring Boot for now. |
There are also media types for error details (e.g. "application/problem+json") defined in RFC 7807 that could be added by a client. @bclozel one extra option is to not render error details at all. The original error message is already lost as it is. This will at least ensure the original status code is not lost as well. That said rendering error details in JSON might not be so bad either since arguably clients don't depend on parsing an error response like they do for success. In the worst case they'll fail to understand the error details but at least they have the status code and some indication about the error that can be read by a human. |
Yes, not rendering the error could be an improvement here. In Spring Boot we're using an annotated I haven't seen So far, we only have the media type for that in our |
I was thinking, if the supported media types for error details are known, then As for the type field from RFC 7807, it says that it is optional, so to start it could be blank. Perhaps some sort of configurable Exception type to URL mapping could be used to allow configuring this further. Adding support for such a type field to existing exception would be a challenge, but it could be an option for |
I think the last is crucial: most applications will catch on any status code != 200 OK, and then forward or log the error (and message body) to the developer. So it's more important to retain the original http error status code here! (as clients may add custom error handling on that integer). But clients won't hardly ever parse the error-message-freetext payload, which we thus should less focus on. Swallowing the original error with a |
I've created spring-projects/spring-boot#19522 to improve that behavior in Spring Boot. I've also created spring-projects/spring-boot#19525 to consider the problem details RFC. |
If a client sends a request with
accept
header not matching the endpointsproduces
MediaType
: should the webservice respond with 406 not acceptable (eg when ConstraintValidationExceptions occur)? - this is the current implementation. In cause you would want to stick to it, the issue can be closed directly.If not - my suggestion: wouldn't it be better if spring would expose the real underlying error eg as message body, while still preserving the 406?
Example to reproduce:
If my endpoint does not produce json or xml, but any other media type like
text/plain
, ortext/event-stream
, then request validations are return asHttpMediaTypeNotAcceptableException
.Which is wrong, they should at least return the real error somehow.
Example:
If the first method is called:
localhost:8080/test?name=
withAccept:application/json
:Then the correct error response is shown:
But for the 2nd method, the real validation error is hidden behind a
406 Not Acceptable
:localhost:8080/test2?name=
withAccept:text/plain
.Returns
406
status code with empty message body.I assume this is because spring tries to return the validation error as another media type that is not
text/plain
, and then fails.I think even if the client requests an
accept
http header explicit, errors should still be getting exposed somehow!Error in logs:
spring-boot-2.1.6
withspring-webmvc-5.1.8
.The text was updated successfully, but these errors were encountered: