Skip to content

Commit

Permalink
Add error request attribute for 500 reponses
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rstoyanchev committed Sep 10, 2012
1 parent 787d8f5 commit 48b963a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ else if (ex instanceof BindException) {
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body,
HttpHeaders headers, HttpStatus status, WebRequest request) {

if (HttpStatus.INTERNAL_SERVER_ERROR.equals(status)) {
request.setAttribute("javax.servlet.error.exception", ex, WebRequest.SCOPE_REQUEST);
}

return new ResponseEntity<Object>(body, headers, status);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,21 @@ protected ModelAndView handleServletRequestBindingException(ServletRequestBindin
protected ModelAndView handleConversionNotSupported(ConversionNotSupportedException ex,
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {

response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
sendServerError(ex, request, response);
return new ModelAndView();
}

/**
* Invoked to send a server error. Sets the status to 500 and also sets the
* request attribute "javax.servlet.error.exception" to the Exception.
*/
protected void sendServerError(Exception ex,
HttpServletRequest request, HttpServletResponse response) throws IOException {

request.setAttribute("javax.servlet.error.exception", ex);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}

/**
* Handle the case when a {@link org.springframework.web.bind.WebDataBinder} conversion error occurs.
* <p>The default implementation sends an HTTP 400 error, and returns an empty {@code ModelAndView}.
Expand Down Expand Up @@ -352,7 +363,7 @@ protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableExcept
protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex,
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {

response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
sendServerError(ex, request, response);
return new ModelAndView();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -204,6 +205,12 @@ public void controllerAdvice() throws Exception {

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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

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;
Expand Down Expand Up @@ -169,6 +171,19 @@ public void handleBindException() throws Exception {
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) {
}

Expand Down

0 comments on commit 48b963a

Please sign in to comment.