Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,30 @@ protected ResponseEntity<?> handleException(Exception exception, HttpServletRequ
// This attribute is never set in MockMvc, so it's not covered in integration test.
request.removeAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);

exception = resolveExceptionCause(exception);
RestExceptionHandler<Exception, ?> handler = resolveExceptionHandler(exception.getClass());

LOG.debug("Handling exception {} with response factory: {}", exception.getClass().getName(), handler);
return handler.handleException(exception, request);
}

protected Exception resolveExceptionCause(Exception exception) {

Throwable lastCause = null;

for(Throwable cause = exception.getCause(); cause != null && (lastCause == null || !cause.equals(lastCause) && !cause.getClass().equals(lastCause.getClass())); cause = cause.getCause()) {
Class clazz1 = cause.getClass();
if(this.handlers.containsKey(clazz1)) {
return (Exception) cause;
}

lastCause = cause;
}

return exception;

}

@SuppressWarnings("unchecked")
protected RestExceptionHandler<Exception, ?> resolveExceptionHandler(Class<? extends Exception> exceptionClass) {

Expand Down