Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Find an Exception cause that we can handle. #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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