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 5ec93af76c..b60f0b3fa7 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 @@ -180,14 +180,32 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException } private void addExceptionErrorMessage(Map errorAttributes, WebRequest webRequest, Throwable error) { + errorAttributes.put("message", getMessage(webRequest, error)); + } + + /** + * Returns the message to be included as the value of the {@code message} error + * attribute. By default the returned message is the first of the following that is + * not empty: + *
    + *
  1. Value of the {@link RequestDispatcher#ERROR_MESSAGE} request attribute. + *
  2. Message of the given {@code error}. + *
  3. {@code No message available}. + *
+ * @param webRequest current request + * @param error current error, if any + * @return message to include in the error attributes + * @since 2.4.0 + */ + protected String getMessage(WebRequest webRequest, Throwable error) { Object message = getAttribute(webRequest, RequestDispatcher.ERROR_MESSAGE); - if (StringUtils.isEmpty(message) && error != null) { - message = error.getMessage(); + if (!StringUtils.isEmpty(message)) { + return message.toString(); } - if (StringUtils.isEmpty(message)) { - message = "No message available"; + if (error != null && !StringUtils.isEmpty(error.getMessage())) { + return error.getMessage(); } - errorAttributes.put("message", message); + return "No message available"; } private void addBindingResultErrorMessage(Map errorAttributes, BindingResult result) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributesTests.java index 22ac9eee84..b8d6c8ac4a 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributesTests.java @@ -257,4 +257,17 @@ class DefaultErrorAttributesTests { assertThat(attributes.get("path")).isEqualTo("path"); } + @Test + void whenGetMessageIsOverridenThenMessageAttributeContainsValueReturnedFromIt() { + Map attributes = new DefaultErrorAttributes() { + + @Override + protected String getMessage(WebRequest webRequest, Throwable error) { + return "custom message"; + } + + }.getErrorAttributes(this.webRequest, ErrorAttributeOptions.of(Include.MESSAGE)); + assertThat(attributes).containsEntry("message", "custom message"); + } + }