From 7bac7370c8351de1d3d12042a0ae6688a0fca50f Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 27 Oct 2014 10:50:10 +0000 Subject: [PATCH] Use request attribute if available for error message Fixes gh-1762, gh-1731 --- .../autoconfigure/web/DefaultErrorAttributes.java | 3 ++- .../web/DefaultErrorAttributesTests.java | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributes.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributes.java index 975c25376d..821c0a9fef 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributes.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributes.java @@ -75,6 +75,7 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException private void storeErrorAttributes(HttpServletRequest request, Exception ex) { request.setAttribute(ERROR_ATTRIBUTE, ex); + } @Override @@ -120,7 +121,7 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException addStackTrace(errorAttributes, error); } } - else { + if (error==null || errorAttributes.get("message")==null) { Object message = getAttribute(requestAttributes, "javax.servlet.error.message"); errorAttributes.put("message", message == null ? "No message available" diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributesTests.java index 61311df309..781f6f133e 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributesTests.java @@ -118,6 +118,17 @@ public class DefaultErrorAttributesTests { assertThat(attributes.get("message"), equalTo((Object) "Test")); } + @Test + public void nullMessage() throws Exception { + this.request.setAttribute("javax.servlet.error.exception", new RuntimeException()); + this.request.setAttribute("javax.servlet.error.message", "Test"); + Map attributes = this.errorAttributes.getErrorAttributes( + this.requestAttributes, false); + assertThat(attributes.get("exception"), + equalTo((Object) RuntimeException.class.getName())); + assertThat(attributes.get("message"), equalTo((Object) "Test")); + } + @Test public void unwrapServletException() throws Exception { RuntimeException ex = new RuntimeException("Test");