@ExceptionHandler is able to process Error thrown from handler method

Issue: SPR-11106
This commit is contained in:
Juergen Hoeller
2016-03-31 11:52:36 +02:00
parent 14bf6509ec
commit f6cb30b165
5 changed files with 89 additions and 38 deletions

View File

@@ -972,13 +972,19 @@ public class DispatcherServlet extends FrameworkServlet {
catch (Exception ex) {
dispatchException = ex;
}
catch (Error err) {
// As of 4.3, we're processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new NestedServletException("Handler dispatch failed", err);
}
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
catch (Exception ex) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
}
catch (Error err) {
triggerAfterCompletionWithError(processedRequest, response, mappedHandler, err);
triggerAfterCompletion(processedRequest, response, mappedHandler,
new NestedServletException("Handler processing failed", err));
}
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
@@ -1304,16 +1310,6 @@ public class DispatcherServlet extends FrameworkServlet {
throw ex;
}
private void triggerAfterCompletionWithError(HttpServletRequest request, HttpServletResponse response,
HandlerExecutionChain mappedHandler, Error error) throws Exception {
ServletException ex = new NestedServletException("Handler processing failed", error);
if (mappedHandler != null) {
mappedHandler.triggerAfterCompletion(request, response, ex);
}
throw ex;
}
/**
* Restore the request attributes after an include.
* @param request current HTTP request

View File

@@ -95,7 +95,7 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
public ExceptionHandlerExceptionResolver() {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // See SPR-7316
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
this.messageConverters = new ArrayList<HttpMessageConverter<?>>();
this.messageConverters.add(new ByteArrayHttpMessageConverter());
@@ -364,7 +364,15 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
if (logger.isDebugEnabled()) {
logger.debug("Invoking @ExceptionHandler method: " + exceptionHandlerMethod);
}
exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
if (exception.getCause() != null) {
// Expose root cause as provided argument as well
exceptionHandlerMethod.invokeAndHandle(
webRequest, mavContainer, exception, exception.getCause(), handlerMethod);
}
else {
// Otherwise, just the given exception as-is
exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
}
}
catch (Exception invocationEx) {
if (logger.isDebugEnabled()) {