Skip to content

Commit 88379e2

Browse files
author
Dave Syer
committed
Check if response is committed before tryying to forward
It' sthe best we can do, but the container seems to still use our error page (it just can't have the status code that we asked for). Fixes gh-748
1 parent a866358 commit 88379e2

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import javax.servlet.http.HttpServletResponse;
3131
import javax.servlet.http.HttpServletResponseWrapper;
3232

33+
import org.apache.commons.logging.Log;
34+
import org.apache.commons.logging.LogFactory;
3335
import org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer;
3436
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
3537
import org.springframework.boot.context.embedded.ErrorPage;
@@ -55,6 +57,8 @@
5557
class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer implements
5658
Filter, NonEmbeddedServletContainerFactory {
5759

60+
private static Log logger = LogFactory.getLog(ErrorPageFilter.class);
61+
5862
// From RequestDispatcher but not referenced to remain compatible with Servlet 2.5
5963

6064
private static final String ERROR_EXCEPTION = "javax.servlet.error.exception";
@@ -132,7 +136,18 @@ private void handleException(HttpServletRequest request,
132136
request.setAttribute(ERROR_EXCEPTION, ex);
133137
request.setAttribute(ERROR_EXCEPTION_TYPE, type.getName());
134138
wrapped.sendError(500, ex.getMessage());
135-
request.getRequestDispatcher(errorPath).forward(request, response);
139+
if (!wrapped.isCommitted()) {
140+
wrapped.reset();
141+
request.getRequestDispatcher(errorPath).forward(request, wrapped);
142+
}
143+
else {
144+
String message = "Cannot forward to error page for"
145+
+ request.getRequestURI()
146+
+ " (response is committed), so this response may have the wrong status code";
147+
logger.error(message);
148+
// User might see the error page without all the data here but the exception
149+
// isn't going to help anyone (and it's already been logged)
150+
}
136151
}
137152

138153
private String getErrorPath(Map<Integer, String> map, Integer status) {

0 commit comments

Comments
 (0)