Merge pull request #22750 from Lopfest
* pr/22750: Polish "Fix include exception handling in DefaultErrorAttributes" Fix include exception handling in DefaultErrorAttributes Closes gh-22750
This commit is contained in:
commit
b8a8b85a58
|
@ -86,7 +86,7 @@ public class DefaultErrorAttributes implements ErrorAttributes {
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
|
public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
|
||||||
Map<String, Object> errorAttributes = getErrorAttributes(request, options.isIncluded(Include.STACK_TRACE));
|
Map<String, Object> errorAttributes = getErrorAttributes(request, options.isIncluded(Include.STACK_TRACE));
|
||||||
if (this.includeException != null) {
|
if (Boolean.TRUE.equals(this.includeException)) {
|
||||||
options = options.including(Include.EXCEPTION);
|
options = options.including(Include.EXCEPTION);
|
||||||
}
|
}
|
||||||
if (!options.isIncluded(Include.EXCEPTION)) {
|
if (!options.isIncluded(Include.EXCEPTION)) {
|
||||||
|
|
|
@ -108,7 +108,7 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
|
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
|
||||||
Map<String, Object> errorAttributes = getErrorAttributes(webRequest, options.isIncluded(Include.STACK_TRACE));
|
Map<String, Object> errorAttributes = getErrorAttributes(webRequest, options.isIncluded(Include.STACK_TRACE));
|
||||||
if (this.includeException != null) {
|
if (Boolean.TRUE.equals(this.includeException)) {
|
||||||
options = options.including(Include.EXCEPTION);
|
options = options.including(Include.EXCEPTION);
|
||||||
}
|
}
|
||||||
if (!options.isIncluded(Include.EXCEPTION)) {
|
if (!options.isIncluded(Include.EXCEPTION)) {
|
||||||
|
|
|
@ -164,6 +164,19 @@ class DefaultErrorAttributesTests {
|
||||||
assertThat(attributes.get("message")).isEqualTo("Test");
|
assertThat(attributes.get("message")).isEqualTo("Test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
void excludeExceptionWithDeprecatedConstructor() {
|
||||||
|
RuntimeException error = new RuntimeException("Test");
|
||||||
|
this.errorAttributes = new DefaultErrorAttributes(false);
|
||||||
|
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
|
||||||
|
ServerRequest serverRequest = buildServerRequest(request, error);
|
||||||
|
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(serverRequest,
|
||||||
|
ErrorAttributeOptions.of());
|
||||||
|
assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error);
|
||||||
|
assertThat(attributes.get("exception")).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void processResponseStatusException() {
|
void processResponseStatusException() {
|
||||||
RuntimeException nested = new RuntimeException("Test");
|
RuntimeException nested = new RuntimeException("Test");
|
||||||
|
|
|
@ -231,6 +231,17 @@ class DefaultErrorAttributesTests {
|
||||||
assertThat(attributes.get("message")).isEqualTo("Test");
|
assertThat(attributes.get("message")).isEqualTo("Test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
void excludeExceptionAttributeWithDeprecatedConstructor() {
|
||||||
|
DefaultErrorAttributes errorAttributes = new DefaultErrorAttributes(false);
|
||||||
|
RuntimeException ex = new RuntimeException("Test");
|
||||||
|
this.request.setAttribute("javax.servlet.error.exception", ex);
|
||||||
|
Map<String, Object> attributes = errorAttributes.getErrorAttributes(this.webRequest,
|
||||||
|
ErrorAttributeOptions.of());
|
||||||
|
assertThat(attributes.get("exception")).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void withStackTraceAttribute() {
|
void withStackTraceAttribute() {
|
||||||
RuntimeException ex = new RuntimeException("Test");
|
RuntimeException ex = new RuntimeException("Test");
|
||||||
|
|
Loading…
Reference in New Issue