|
30 | 30 | import javax.servlet.http.HttpServletResponse;
|
31 | 31 | import javax.servlet.http.HttpServletResponseWrapper;
|
32 | 32 |
|
| 33 | +import org.apache.commons.logging.Log; |
| 34 | +import org.apache.commons.logging.LogFactory; |
33 | 35 | import org.springframework.boot.context.embedded.AbstractConfigurableEmbeddedServletContainer;
|
34 | 36 | import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
35 | 37 | import org.springframework.boot.context.embedded.ErrorPage;
|
|
55 | 57 | class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer implements
|
56 | 58 | Filter, NonEmbeddedServletContainerFactory {
|
57 | 59 |
|
| 60 | + private static Log logger = LogFactory.getLog(ErrorPageFilter.class); |
| 61 | + |
58 | 62 | // From RequestDispatcher but not referenced to remain compatible with Servlet 2.5
|
59 | 63 |
|
60 | 64 | private static final String ERROR_EXCEPTION = "javax.servlet.error.exception";
|
@@ -132,7 +136,18 @@ private void handleException(HttpServletRequest request,
|
132 | 136 | request.setAttribute(ERROR_EXCEPTION, ex);
|
133 | 137 | request.setAttribute(ERROR_EXCEPTION_TYPE, type.getName());
|
134 | 138 | 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 | + } |
136 | 151 | }
|
137 | 152 |
|
138 | 153 | private String getErrorPath(Map<Integer, String> map, Integer status) {
|
|
0 commit comments