@ExceptionHandler is able to process Error thrown from handler method
Issue: SPR-11106
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -42,12 +42,9 @@ import org.springframework.web.method.annotation.ModelMethodProcessor;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.util.NestedServletException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test fixture with {@link ExceptionHandlerExceptionResolver}.
|
||||
@@ -240,6 +237,22 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||
assertEquals("HandlerMethod: handle", this.response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveExceptionWithAssertionError() throws Exception {
|
||||
AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);
|
||||
this.resolver.setApplicationContext(cxt);
|
||||
this.resolver.afterPropertiesSet();
|
||||
|
||||
AssertionError err = new AssertionError("argh");
|
||||
HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle");
|
||||
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod,
|
||||
new NestedServletException("Handler dispatch failed", err));
|
||||
|
||||
assertNotNull("Exception was not handled", mav);
|
||||
assertTrue(mav.isEmpty());
|
||||
assertEquals(err.toString(), this.response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveExceptionControllerAdviceHandler() throws Exception {
|
||||
AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyControllerAdviceConfig.class);
|
||||
@@ -349,6 +362,11 @@ public class ExceptionHandlerExceptionResolverTests {
|
||||
public String handleWithHandlerMethod(HandlerMethod handlerMethod) {
|
||||
return "HandlerMethod: " + handlerMethod.getMethod().getName();
|
||||
}
|
||||
|
||||
@ExceptionHandler(AssertionError.class)
|
||||
public String handleAssertionError(Error err) {
|
||||
return err.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -139,30 +139,23 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* The origin of this test class is {@link ServletAnnotationControllerHandlerMethodTests}.
|
||||
*
|
||||
* Tests in this class run against the {@link HandlerMethod} infrastructure:
|
||||
* <ul>
|
||||
* <li>RequestMappingHandlerMapping
|
||||
* <li>RequestMappingHandlerAdapter
|
||||
* <li>ExceptionHandlerExceptionResolver
|
||||
* <li>RequestMappingHandlerMapping
|
||||
* <li>RequestMappingHandlerAdapter
|
||||
* <li>ExceptionHandlerExceptionResolver
|
||||
* </ul>
|
||||
*
|
||||
* <p>Rather than against the existing infrastructure:
|
||||
* <ul>
|
||||
* <li>DefaultAnnotationHandlerMapping
|
||||
* <li>AnnotationMethodHandlerAdapter
|
||||
* <li>AnnotationMethodHandlerExceptionResolver
|
||||
* <li>DefaultAnnotationHandlerMapping
|
||||
* <li>AnnotationMethodHandlerAdapter
|
||||
* <li>AnnotationMethodHandlerExceptionResolver
|
||||
* </ul>
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -181,6 +174,18 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
|
||||
assertEquals("test", response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void errorThrownFromHandlerMethod() throws Exception {
|
||||
initServletWithControllers(ControllerWithErrorThrown.class);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
|
||||
request.setContextPath("/foo");
|
||||
request.setServletPath("");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
getServlet().service(request, response);
|
||||
assertEquals("test", response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customAnnotationController() throws Exception {
|
||||
initServletWithControllers(CustomAnnotationController.class);
|
||||
@@ -1808,6 +1813,25 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
|
||||
}
|
||||
}
|
||||
|
||||
@Controller
|
||||
private static class ControllerWithErrorThrown {
|
||||
|
||||
@RequestMapping("")
|
||||
public void myPath2(HttpServletResponse response) throws IOException {
|
||||
throw new AssertionError("test");
|
||||
}
|
||||
|
||||
@RequestMapping("/bar")
|
||||
public void myPath3(HttpServletResponse response) throws IOException {
|
||||
response.getWriter().write("testX");
|
||||
}
|
||||
|
||||
@ExceptionHandler
|
||||
public void myPath2(Error err, HttpServletResponse response) throws IOException {
|
||||
response.getWriter().write(err.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Controller
|
||||
static class MyAdaptedController {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user