Fixed potential ClassCastException getting error

Update DefaultErrorAttributes to expect a `Throwable` ERROR_ATTRIBUTE
rather than an `Exception`.

Fixes gh-1931
This commit is contained in:
juzer 2014-11-16 16:08:34 +01:00 committed by Phillip Webb
parent c34cfb27a3
commit 428d2caac1
2 changed files with 14 additions and 1 deletions

View File

@ -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");
}

View File

@ -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<String, Object> 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",