Browse Source

Fix logic error in ErrorPageFilter (fixes gh-1149)

pull/1165/head
Dave Syer 12 years ago
parent
commit
b0bf9c776f
  1. 5
      spring-boot-samples/spring-boot-sample-web-jsp/src/main/java/sample/jsp/WelcomeController.java
  2. 7
      spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java
  3. 38
      spring-boot/src/test/java/org/springframework/boot/context/web/ErrorPageFilterTests.java

5
spring-boot-samples/spring-boot-sample-web-jsp/src/main/java/sample/jsp/WelcomeController.java

@ -36,4 +36,9 @@ public class WelcomeController { @@ -36,4 +36,9 @@ public class WelcomeController {
return "welcome";
}
@RequestMapping("/foo")
public String foo(Map<String, Object> model) {
throw new RuntimeException("Foo");
}
}

7
spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java

@ -135,13 +135,13 @@ class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer imple @@ -135,13 +135,13 @@ class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer imple
setErrorAttributes(request, 500, ex.getMessage());
request.setAttribute(ERROR_EXCEPTION, ex);
request.setAttribute(ERROR_EXCEPTION_TYPE, type.getName());
wrapped.sendError(500, ex.getMessage());
forwardToErrorPage(errorPath, request, wrapped, ex);
}
private void forwardToErrorPage(String path, HttpServletRequest request,
ServletResponse response, Throwable ex) throws ServletException, IOException {
if (!response.isCommitted()) {
HttpServletResponse response, Throwable ex) throws ServletException,
IOException {
if (response.isCommitted()) {
String message = "Cannot forward to error page for" + request.getRequestURI()
+ " (response is committed), so this response may have "
+ "the wrong status code";
@ -151,6 +151,7 @@ class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer imple @@ -151,6 +151,7 @@ class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer imple
return;
}
response.reset();
response.sendError(500, ex.getMessage());
request.getRequestDispatcher(path).forward(request, response);
}

38
spring-boot/src/test/java/org/springframework/boot/context/web/ErrorPageFilterTests.java

@ -59,6 +59,44 @@ public class ErrorPageFilterTests { @@ -59,6 +59,44 @@ public class ErrorPageFilterTests {
equalTo((ServletResponse) this.response));
}
@Test
public void responseCommitted() throws Exception {
this.filter.addErrorPages(new ErrorPage("/error"));
this.response.setCommitted(true);
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
((HttpServletResponse) response).sendError(400, "BAD");
super.doFilter(request, response);
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(this.chain.getRequest(), equalTo((ServletRequest) this.request));
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse(),
equalTo((ServletResponse) this.response));
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus(),
equalTo(400));
}
@Test
public void responseUncommitted() throws Exception {
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
((HttpServletResponse) response).sendError(400, "BAD");
super.doFilter(request, response);
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(this.chain.getRequest(), equalTo((ServletRequest) this.request));
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getResponse(),
equalTo((ServletResponse) this.response));
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus(),
equalTo(400));
}
@Test
public void globalError() throws Exception {
this.filter.addErrorPages(new ErrorPage("/error"));

Loading…
Cancel
Save