Fix logic error in ErrorPageFilter (fixes gh-1149)

This commit is contained in:
Dave Syer 2014-06-25 08:46:59 +01:00
parent 91bbd20ca0
commit b0bf9c776f
3 changed files with 47 additions and 3 deletions

View File

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

View File

@ -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
return;
}
response.reset();
response.sendError(500, ex.getMessage());
request.getRequestDispatcher(path).forward(request, response);
}

View File

@ -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"));