diff --git a/spring-boot-samples/spring-boot-sample-web-jsp/src/main/java/sample/jsp/WelcomeController.java b/spring-boot-samples/spring-boot-sample-web-jsp/src/main/java/sample/jsp/WelcomeController.java index 03d439caa3c..646c00c3739 100644 --- a/spring-boot-samples/spring-boot-sample-web-jsp/src/main/java/sample/jsp/WelcomeController.java +++ b/spring-boot-samples/spring-boot-sample-web-jsp/src/main/java/sample/jsp/WelcomeController.java @@ -36,4 +36,9 @@ public class WelcomeController { return "welcome"; } + @RequestMapping("/foo") + public String foo(Map model) { + throw new RuntimeException("Foo"); + } + } diff --git a/spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java b/spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java index e31ac849ffe..2edf3cb5387 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/web/ErrorPageFilter.java @@ -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); } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/web/ErrorPageFilterTests.java b/spring-boot/src/test/java/org/springframework/boot/context/web/ErrorPageFilterTests.java index a7e389bb910..24e6ccc3ebc 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/web/ErrorPageFilterTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/web/ErrorPageFilterTests.java @@ -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"));