diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ManagementErrorEndpoint.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ManagementErrorEndpoint.java index 4fd070eb4b..27902c1c79 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ManagementErrorEndpoint.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ManagementErrorEndpoint.java @@ -57,25 +57,25 @@ public class ManagementErrorEndpoint { } private boolean includeStackTrace(ServletWebRequest request) { - ErrorProperties.IncludeStacktrace include = this.errorProperties.getIncludeStacktrace(); - if (include == ErrorProperties.IncludeStacktrace.ALWAYS) { + switch (this.errorProperties.getIncludeStacktrace()) { + case ALWAYS: return true; - } - if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) { + case ON_TRACE_PARAM: return getBooleanParameter(request, "trace"); + default: + return false; } - return false; } private boolean includeDetails(ServletWebRequest request) { - ErrorProperties.IncludeDetails include = this.errorProperties.getIncludeDetails(); - if (include == ErrorProperties.IncludeDetails.ALWAYS) { + switch (this.errorProperties.getIncludeDetails()) { + case ALWAYS: return true; - } - if (include == ErrorProperties.IncludeDetails.ON_DETAILS_PARAM) { + case ON_DETAILS_PARAM: return getBooleanParameter(request, "details"); + default: + return false; } - return false; } protected boolean getBooleanParameter(ServletWebRequest request, String parameterName) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java index 951c87abac..ca49c11cdb 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java @@ -156,14 +156,14 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa * @return if the stacktrace attribute should be included */ protected boolean isIncludeStackTrace(ServerRequest request, MediaType produces) { - ErrorProperties.IncludeStacktrace include = this.errorProperties.getIncludeStacktrace(); - if (include == ErrorProperties.IncludeStacktrace.ALWAYS) { + switch (this.errorProperties.getIncludeStacktrace()) { + case ALWAYS: return true; - } - if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) { + case ON_TRACE_PARAM: return isTraceEnabled(request); + default: + return false; } - return false; } /** @@ -173,14 +173,14 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa * @return if the message and errors attributes should be included */ protected boolean isIncludeDetails(ServerRequest request, MediaType produces) { - ErrorProperties.IncludeDetails include = this.errorProperties.getIncludeDetails(); - if (include == ErrorProperties.IncludeDetails.ALWAYS) { + switch (this.errorProperties.getIncludeDetails()) { + case ALWAYS: return true; - } - if (include == ErrorProperties.IncludeDetails.ON_DETAILS_PARAM) { + case ON_DETAILS_PARAM: return isDetailsEnabled(request); + default: + return false; } - return false; } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorController.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorController.java index d8a9cf4186..fc45e9e552 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorController.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorController.java @@ -24,8 +24,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.boot.autoconfigure.web.ErrorProperties; -import org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeDetails; -import org.springframework.boot.autoconfigure.web.ErrorProperties.IncludeStacktrace; import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory; import org.springframework.http.HttpStatus; @@ -120,14 +118,14 @@ public class BasicErrorController extends AbstractErrorController { * @return if the stacktrace attribute should be included */ protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) { - IncludeStacktrace include = getErrorProperties().getIncludeStacktrace(); - if (include == IncludeStacktrace.ALWAYS) { + switch (getErrorProperties().getIncludeStacktrace()) { + case ALWAYS: return true; - } - if (include == IncludeStacktrace.ON_TRACE_PARAM) { + case ON_TRACE_PARAM: return getTraceParameter(request); + default: + return false; } - return false; } /** @@ -137,14 +135,14 @@ public class BasicErrorController extends AbstractErrorController { * @return if the error details attributes should be included */ protected boolean isIncludeDetails(HttpServletRequest request, MediaType produces) { - IncludeDetails include = getErrorProperties().getIncludeDetails(); - if (include == IncludeDetails.ALWAYS) { + switch (getErrorProperties().getIncludeDetails()) { + case ALWAYS: return true; - } - if (include == IncludeDetails.ON_DETAILS_PARAM) { + case ON_DETAILS_PARAM: return getDetailsParameter(request); + default: + return false; } - return false; } /** diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java index 1d5d717feb..9254a6637c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java @@ -22,6 +22,7 @@ import java.util.Date; import java.util.LinkedHashMap; import java.util.Map; +import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -118,7 +119,7 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException } private void addStatus(Map errorAttributes, RequestAttributes requestAttributes) { - Integer status = getAttribute(requestAttributes, "javax.servlet.error.status_code"); + Integer status = getAttribute(requestAttributes, RequestDispatcher.ERROR_STATUS_CODE); if (status == null) { errorAttributes.put("status", 999); errorAttributes.put("error", "None"); @@ -168,7 +169,7 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException errorAttributes.put("message", "An error occurred while processing the request"); return; } - Object message = getAttribute(webRequest, "javax.servlet.error.message"); + Object message = getAttribute(webRequest, RequestDispatcher.ERROR_MESSAGE); if (StringUtils.isEmpty(message) && error != null) { message = error.getMessage(); } @@ -209,7 +210,7 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException } private void addPath(Map errorAttributes, RequestAttributes requestAttributes) { - String path = getAttribute(requestAttributes, "javax.servlet.error.request_uri"); + String path = getAttribute(requestAttributes, RequestDispatcher.ERROR_REQUEST_URI); if (path != null) { errorAttributes.put("path", path); } @@ -218,10 +219,7 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException @Override public Throwable getError(WebRequest webRequest) { Throwable exception = getAttribute(webRequest, ERROR_ATTRIBUTE); - if (exception == null) { - exception = getAttribute(webRequest, "javax.servlet.error.exception"); - } - return exception; + return (exception != null) ? exception : getAttribute(webRequest, RequestDispatcher.ERROR_EXCEPTION); } @SuppressWarnings("unchecked") diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/ErrorAttributes.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/ErrorAttributes.java index 5dc1015a51..916639f642 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/ErrorAttributes.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/ErrorAttributes.java @@ -53,6 +53,7 @@ public interface ErrorAttributes { * @param includeStackTrace if stack trace elements should be included * @param includeDetails if message and errors elements should be included * @return a map of error attributes + * @since 2.3.0 */ Map getErrorAttributes(WebRequest webRequest, boolean includeStackTrace, boolean includeDetails);