Skip to content

Commit b8a8b85

Browse files
committed
Merge pull request #22750 from Lopfest
* pr/22750: Polish "Fix include exception handling in DefaultErrorAttributes" Fix include exception handling in DefaultErrorAttributes Closes gh-22750
2 parents d452bbb + c2ec46c commit b8a8b85

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public DefaultErrorAttributes(boolean includeException) {
8686
@Override
8787
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
8888
Map<String, Object> errorAttributes = getErrorAttributes(request, options.isIncluded(Include.STACK_TRACE));
89-
if (this.includeException != null) {
89+
if (Boolean.TRUE.equals(this.includeException)) {
9090
options = options.including(Include.EXCEPTION);
9191
}
9292
if (!options.isIncluded(Include.EXCEPTION)) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private void storeErrorAttributes(HttpServletRequest request, Exception ex) {
108108
@Override
109109
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
110110
Map<String, Object> errorAttributes = getErrorAttributes(webRequest, options.isIncluded(Include.STACK_TRACE));
111-
if (this.includeException != null) {
111+
if (Boolean.TRUE.equals(this.includeException)) {
112112
options = options.including(Include.EXCEPTION);
113113
}
114114
if (!options.isIncluded(Include.EXCEPTION)) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributesTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,19 @@ void includeException() {
164164
assertThat(attributes.get("message")).isEqualTo("Test");
165165
}
166166

167+
@Test
168+
@SuppressWarnings("deprecation")
169+
void excludeExceptionWithDeprecatedConstructor() {
170+
RuntimeException error = new RuntimeException("Test");
171+
this.errorAttributes = new DefaultErrorAttributes(false);
172+
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
173+
ServerRequest serverRequest = buildServerRequest(request, error);
174+
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(serverRequest,
175+
ErrorAttributeOptions.of());
176+
assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error);
177+
assertThat(attributes.get("exception")).isNull();
178+
}
179+
167180
@Test
168181
void processResponseStatusException() {
169182
RuntimeException nested = new RuntimeException("Test");

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributesTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@ void withExceptionAttribute() {
231231
assertThat(attributes.get("message")).isEqualTo("Test");
232232
}
233233

234+
@Test
235+
@SuppressWarnings("deprecation")
236+
void excludeExceptionAttributeWithDeprecatedConstructor() {
237+
DefaultErrorAttributes errorAttributes = new DefaultErrorAttributes(false);
238+
RuntimeException ex = new RuntimeException("Test");
239+
this.request.setAttribute("javax.servlet.error.exception", ex);
240+
Map<String, Object> attributes = errorAttributes.getErrorAttributes(this.webRequest,
241+
ErrorAttributeOptions.of());
242+
assertThat(attributes.get("exception")).isNull();
243+
}
244+
234245
@Test
235246
void withStackTraceAttribute() {
236247
RuntimeException ex = new RuntimeException("Test");

0 commit comments

Comments
 (0)