From 428d2caac16436c2afdf1b4321b9c937bcb6ba39 Mon Sep 17 00:00:00 2001 From: juzer Date: Sun, 16 Nov 2014 16:08:34 +0100 Subject: [PATCH] Fixed potential ClassCastException getting error Update DefaultErrorAttributes to expect a `Throwable` ERROR_ATTRIBUTE rather than an `Exception`. Fixes gh-1931 --- .../autoconfigure/web/DefaultErrorAttributes.java | 2 +- .../web/DefaultErrorAttributesTests.java | 13 +++++++++++++ 2 files changed, 14 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 9ea8f70bfd..c6d3a35ef9 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 @@ -163,7 +163,7 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException @Override public Throwable getError(RequestAttributes requestAttributes) { - Exception exception = getAttribute(requestAttributes, ERROR_ATTRIBUTE); + Throwable exception = getAttribute(requestAttributes, ERROR_ATTRIBUTE); if (exception == null) { exception = getAttribute(requestAttributes, "javax.servlet.error.exception"); } 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 1d3e8173c4..e6c0c64257 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 @@ -144,6 +144,19 @@ public class DefaultErrorAttributesTests { assertThat(attributes.get("message"), equalTo((Object) "Test")); } + @Test + public void getError() throws Exception { + Error error = new OutOfMemoryError("Test error"); + this.request.setAttribute("javax.servlet.error.exception", error); + Map attributes = this.errorAttributes.getErrorAttributes( + this.requestAttributes, false); + assertThat(this.errorAttributes.getError(this.requestAttributes), + sameInstance((Object) error)); + assertThat(attributes.get("exception"), + equalTo((Object) OutOfMemoryError.class.getName())); + assertThat(attributes.get("message"), equalTo((Object) "Test error")); + } + @Test public void extractBindingResultErrors() throws Exception { BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a",