I have following Exception Handler:
@Log4j2
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity handleException(Exception e) {
logExceptionWithPath("Unhandled general exception", e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
@ResponseBody
@ExceptionHandler({ValidationException.class})
public ResponseEntity<ErrorResponse> handleBindException(ValidationException ex) {
logExceptionWithPath("Validation exception", ex);
return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST);
}
@ResponseBody
@ExceptionHandler({BindException.class})
public ResponseEntity<ErrorResponse> handleBindException(BindException ex) {
logExceptionWithPath("Bind exception", ex);
return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST);
}
@ResponseBody
@ExceptionHandler(AmbiguousTermException.class)
public ResponseEntity<ErrorResponse> handleAmbiguousTermException(AmbiguousTermException ex) {
logExceptionWithPath("AmbiguousTermException", ex);
return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST);
}
This code should wrap exceptions and return ResponseEntity with correct Status code.
But instead, I always get this:
{
"timestamp": 1560445348350,
"status": 500,
"error": "Internal Server Error",
"message": "I/O error on POST request for \"******": Error code:400 Message: ; nested exception is java.io.IOException: Error code:400 Message: ",
"path": "*****"
}
In logs I have always messages produced by logExceptionWithPath, so it seems like ExceptionHandler is working fine, but then there is IOException somewhere.
This response is similar for each exception (and each @ExceptionHandler)
below few logs from my app and spring:
2019-06-13 17:26:00,237 DEBUG *:102 - assertTermsUnequivocal
2019-06-13 17:26:00,237 DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver:403 - Using @ExceptionHandler public org.springframework.http.ResponseEntity<*.ErrorResponse> *.GlobalExceptionHandler.handleAmbiguousTermException(*.AmbiguousTermException)
2019-06-13 17:26:00,238 ERROR *.GlobalExceptionHandler:92 - AmbiguousTermException for path: http://*/search
here stack trace from AmbiguousTermException
2019-06-13 17:26:00,239 DEBUG org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor:268 - Using 'application/json', given [text/plain, application/json, application/cbor, application/*+json, */*] and supported [application/json, application/*+json, application/json, application/*+json, application/cbor]
2019-06-13 17:26:00,239 DEBUG org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor:90 - Writing [*.ErrorResponse@17bd7832]
2019-06-13 17:26:00,240 DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver:143 - Resolved [*.AmbiguousTermException: terms are ambiguous]
2019-06-13 17:26:00,240 DEBUG org.springframework.web.servlet.DispatcherServlet:1130 - Completed 400 BAD_REQUEST
That's all from interesting logs. Maybe this is not connected to Spring at all, but rather servers it is running? Anyway if you have any clues why it don't work, please leave an answer or comment.