-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Make DefaultErrorAttributes easier to subclass for message customization #22378
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
Comments
Thanks for the report. This an interesting difference in servlet container behaviour. Spring Framework's Unfortunately, there's no way for Spring Boot to know if it should prefer the exception's error message or the value of the You can customise the behaviour described above by providing your own @Bean
public ErrorAttributes customErrorAttributes() {
return new DefaultErrorAttributes() {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
Map<String, Object> attributes = super.getErrorAttributes(webRequest, options);
Throwable error = getError(webRequest);
if (error == null) {
return attributes;
}
String message = error.getMessage();
if (StringUtils.isEmpty(message)) {
message = getAttribute(webRequest, RequestDispatcher.ERROR_MESSAGE);
}
if (StringUtils.isEmpty(message)) {
message = "No message available";
}
attributes.put("message", message);
return attributes;
}
@SuppressWarnings("unchecked")
private <T> T getAttribute(RequestAttributes requestAttributes, String name) {
return (T) requestAttributes.getAttribute(name, RequestAttributes.SCOPE_REQUEST);
}
};
} |
@wilkinsona |
I have an exception like this
And then ,I throw the exception in a controller
if I use tomcat containner ,the response is like this
but if I use undertow or jetty containner ,the response is like this
there is different with message field.
I think it a Bug.
The text was updated successfully, but these errors were encountered: