Skip to content

Commit 26f5912

Browse files
committed
Fix handling of NestedServletException with no root cause
Fixes gh-22169
1 parent c9958c2 commit 26f5912

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/support/ErrorPageFilter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -137,7 +137,10 @@ else if (!request.isAsyncStarted() && !response.isCommitted()) {
137137
catch (Throwable ex) {
138138
Throwable exceptionToHandle = ex;
139139
if (ex instanceof NestedServletException) {
140-
exceptionToHandle = ((NestedServletException) ex).getRootCause();
140+
Throwable rootCause = ((NestedServletException) ex).getRootCause();
141+
if (rootCause != null) {
142+
exceptionToHandle = rootCause;
143+
}
141144
}
142145
handleException(request, response, wrapped, exceptionToHandle);
143146
response.flushBuffer();

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/support/ErrorPageFilterTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@
4242
import org.springframework.mock.web.MockHttpServletRequest;
4343
import org.springframework.mock.web.MockHttpServletResponse;
4444
import org.springframework.mock.web.MockRequestDispatcher;
45+
import org.springframework.web.bind.MissingServletRequestParameterException;
4546
import org.springframework.web.context.request.async.DeferredResult;
4647
import org.springframework.web.context.request.async.StandardServletAsyncWebRequest;
4748
import org.springframework.web.context.request.async.WebAsyncManager;
@@ -388,6 +389,30 @@ public void nestedServletExceptionIsUnwrapped() throws Exception {
388389
assertThat(this.response.getForwardedUrl()).isEqualTo("/500");
389390
}
390391

392+
@Test
393+
public void nestedServletExceptionWithNoCause() throws Exception {
394+
this.filter.addErrorPages(new ErrorPage(MissingServletRequestParameterException.class, "/500"));
395+
this.chain = new TestFilterChain((request, response, chain) -> {
396+
chain.call();
397+
throw new MissingServletRequestParameterException("test", "string");
398+
});
399+
this.filter.doFilter(this.request, this.response, this.chain);
400+
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(500);
401+
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)).isEqualTo(500);
402+
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE))
403+
.isEqualTo("Required string parameter 'test' is not present");
404+
Map<String, Object> requestAttributes = getAttributesForDispatch("/500");
405+
assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION_TYPE))
406+
.isEqualTo(MissingServletRequestParameterException.class);
407+
assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION))
408+
.isInstanceOf(MissingServletRequestParameterException.class);
409+
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isNull();
410+
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION)).isNull();
411+
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)).isEqualTo("/test/path");
412+
assertThat(this.response.isCommitted()).isTrue();
413+
assertThat(this.response.getForwardedUrl()).isEqualTo("/500");
414+
}
415+
391416
@Test
392417
public void whenErrorIsSentAndWriterIsFlushedErrorIsSentToTheClient() throws Exception {
393418
this.chain = new TestFilterChain((request, response, chain) -> {

0 commit comments

Comments
 (0)