Description
Version: 2.1.10.RELEASE
After upgrading from spring boot 2.1.8 to 2.1.10 we start retrieving HTTP 500 errors instead of HTTP 400 or 409 that were thrown before. After some investigation we've found custom error handling in our code base for some of the errors thrown by spring jpa/hibernate/mongo/etc which was detected in our custom implementation of ErrorAttributes and apart of returning custom error message we were also modifying 'javax.servlet.error.status_code' attribute of the WebRequest sent as input parameter. This was working so far without any problem because response status was determined after resolving error attributes as you can see below.
public class BasicErrorController extends AbstractErrorController {
@RequestMapping
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL)); // HERE we could still override javax.servlet.error.status_code which is used as HTTP response status
HttpStatus status = getStatus(request);
return new ResponseEntity<>(body, status);
}
With latest code the status is resolved before retrieving error attributes, and hence overriding response status has no effect and the old code no longer works for us. Please restore the original behavior so that overriding javax.servlet.error.status_code in ErrorAttributes:getErrorAttributes affects the response status returned in ResponseEntity.
Workaround
As a workaround we already extended BasicErrorController and override its error method:
@Override
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = getStatus(request);
return status == HttpStatus.NO_CONTENT ? new ResponseEntity<>(status) : new ResponseEntity<>(body, status);
}
however I believe it would make much more sense to modify the BasicErrorController so that people relying on similar behavior can still use this.