From de48223a2ebd8da2677c9f5faa0bc63a6f0bbeb3 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 11 Sep 2015 12:27:39 -0700 Subject: [PATCH] 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 --- .../boot/context/web/ErrorPageFilter.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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 c772c3ba2ac..2c247507c66 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 @@ -114,9 +114,9 @@ public class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContaine ErrorWrapperResponse wrapped = new ErrorWrapperResponse(response); try { chain.doFilter(request, wrapped); - int status = wrapped.getStatus(); - if (status >= 400) { - handleErrorStatus(request, response, status, wrapped.getMessage()); + if (wrapped.hasErrorToSend()) { + handleErrorStatus(request, response, wrapped.getStatus(), + wrapped.getMessage()); response.flushBuffer(); } else if (!request.isAsyncStarted() && !response.isCommitted()) { @@ -140,7 +140,6 @@ public class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContaine handleCommittedResponse(request, null); return; } - String errorPath = getErrorPath(this.statuses, status); if (errorPath == null) { response.sendError(status, message); @@ -321,6 +320,10 @@ public class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContaine return this.message; } + public boolean hasErrorToSend() { + return this.hasErrorToSend; + } + } }