Skip to content

Commit de48223

Browse files
committed
Only handle status errors when sendError is called
Update ErrorPageFilter to only handle errors when `response.sendError` has been called. This should allow custom @exceptionhandlers to completely handle errors and return custom status codes without triggering the "Cannot forward to error page" log message. The Javadoc for sendError states: "The server defaults to creating the response to look like an HTML-formatted server error page containing the specified message" Where as setStatus states "This method is used to set the return status code when there is no error " Fixes gh-2745
1 parent 5f250eb commit de48223

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ private void doFilter(HttpServletRequest request, HttpServletResponse response,
114114
ErrorWrapperResponse wrapped = new ErrorWrapperResponse(response);
115115
try {
116116
chain.doFilter(request, wrapped);
117-
int status = wrapped.getStatus();
118-
if (status >= 400) {
119-
handleErrorStatus(request, response, status, wrapped.getMessage());
117+
if (wrapped.hasErrorToSend()) {
118+
handleErrorStatus(request, response, wrapped.getStatus(),
119+
wrapped.getMessage());
120120
response.flushBuffer();
121121
}
122122
else if (!request.isAsyncStarted() && !response.isCommitted()) {
@@ -140,7 +140,6 @@ private void handleErrorStatus(HttpServletRequest request,
140140
handleCommittedResponse(request, null);
141141
return;
142142
}
143-
144143
String errorPath = getErrorPath(this.statuses, status);
145144
if (errorPath == null) {
146145
response.sendError(status, message);
@@ -321,6 +320,10 @@ public String getMessage() {
321320
return this.message;
322321
}
323322

323+
public boolean hasErrorToSend() {
324+
return this.hasErrorToSend;
325+
}
326+
324327
}
325328

326329
}

0 commit comments

Comments
 (0)