@ExceptionHandler matches against cause type as well

Issue: SPR-14291
This commit is contained in:
Juergen Hoeller
2016-06-07 21:22:01 +02:00
parent 99ae7e3bcb
commit 981c894acf
4 changed files with 38 additions and 21 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@ public abstract class AbstractHandlerMethodExceptionResolver extends AbstractHan
/**
* Checks if the handler is a {@link HandlerMethod} and then delegates to the
* base class implementation of {@link #shouldApplyTo(HttpServletRequest, Object)}
* base class implementation of {@code #shouldApplyTo(HttpServletRequest, Object)}
* passing the bean of the {@code HandlerMethod}. Otherwise returns {@code false}.
*/
@Override
@@ -54,8 +54,7 @@ public abstract class AbstractHandlerMethodExceptionResolver extends AbstractHan
@Override
protected final ModelAndView doResolveException(
HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
return doResolveHandlerMethodException(request, response, (HandlerMethod) handler, ex);
}
@@ -75,7 +74,6 @@ public abstract class AbstractHandlerMethodExceptionResolver extends AbstractHan
* @return a corresponding ModelAndView to forward to, or {@code null} for default processing
*/
protected abstract ModelAndView doResolveHandlerMethodException(
HttpServletRequest request, HttpServletResponse response,
HandlerMethod handlerMethod, Exception ex);
HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, Exception ex);
}

View File

@@ -65,6 +65,7 @@ import org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionRes
* {@link #setArgumentResolvers} and {@link #setReturnValueHandlers(List)}.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 3.1
*/
public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExceptionResolver
@@ -364,10 +365,10 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
if (logger.isDebugEnabled()) {
logger.debug("Invoking @ExceptionHandler method: " + exceptionHandlerMethod);
}
if (exception.getCause() != null) {
// Expose root cause as provided argument as well
exceptionHandlerMethod.invokeAndHandle(
webRequest, mavContainer, exception, exception.getCause(), handlerMethod);
Throwable cause = exception.getCause();
if (cause != null) {
// Expose cause as provided argument as well
exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, cause, handlerMethod);
}
else {
// Otherwise, just the given exception as-is