Add error request attribute for 500 reponses

DefaultHandlerExceptionResolver and ResponseEntityExceptionHandler now
both set the "javax.servlet.error.exception" request attribute to the
raised exception, allowing custom error pages configured via web.xml.

Issue: SPR-9653
This commit is contained in:
Rossen Stoyanchev
2012-09-10 18:22:28 -04:00
parent 787d8f5916
commit 48b963aaa5
4 changed files with 39 additions and 2 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.web.servlet.mvc.method.annotation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Method;
@@ -204,6 +205,12 @@ public class ResponseEntityExceptionHandlerTests {
private ResponseEntity<Object> testException(Exception ex) {
ResponseEntity<Object> responseEntity = this.exceptionHandlerSupport.handleException(ex, this.request);
// SPR-9653
if (HttpStatus.INTERNAL_SERVER_ERROR.equals(responseEntity.getStatusCode())) {
assertSame(ex, this.servletRequest.getAttribute("javax.servlet.error.exception"));
}
this.defaultExceptionResolver.resolveException(this.servletRequest, this.servletResponse, null, ex);
assertEquals(this.servletResponse.getStatus(), responseEntity.getStatusCode().value());

View File

@@ -18,12 +18,14 @@ package org.springframework.web.servlet.mvc.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TestBean;
import org.springframework.beans.TypeMismatchException;
import org.springframework.core.MethodParameter;
@@ -169,6 +171,19 @@ public class DefaultHandlerExceptionResolverTests {
assertEquals("Invalid status code", 400, response.getStatus());
}
@Test
public void handleConversionNotSupportedException() throws Exception {
ConversionNotSupportedException ex =
new ConversionNotSupportedException(new Object(), String.class, new Exception());
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
assertNotNull("No ModelAndView returned", mav);
assertTrue("No Empty ModelAndView returned", mav.isEmpty());
assertEquals("Invalid status code", 500, response.getStatus());
// SPR-9653
assertSame(ex, request.getAttribute("javax.servlet.error.exception"));
}
public void handle(String arg) {
}