Skip to content

Commit 14f24db

Browse files
committed
Merge branch '2.1.x' into 2.2.x
Closes gh-22248
2 parents 1ad66cf + 26f5912 commit 14f24db

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.
@@ -43,6 +43,7 @@
4343
import org.springframework.mock.web.MockHttpServletRequest;
4444
import org.springframework.mock.web.MockHttpServletResponse;
4545
import org.springframework.mock.web.MockRequestDispatcher;
46+
import org.springframework.web.bind.MissingServletRequestParameterException;
4647
import org.springframework.web.context.request.async.DeferredResult;
4748
import org.springframework.web.context.request.async.StandardServletAsyncWebRequest;
4849
import org.springframework.web.context.request.async.WebAsyncManager;
@@ -387,6 +388,30 @@ void nestedServletExceptionIsUnwrapped() throws Exception {
387388
assertThat(this.response.getForwardedUrl()).isEqualTo("/500");
388389
}
389390

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

0 commit comments

Comments
 (0)