Remove error message attribute instead of blanking it when not included

Closes gh-24712
This commit is contained in:
Andy Wilkinson 2021-01-22 14:10:02 +00:00
parent 1370e8ca97
commit 5a56f8864f
7 changed files with 23 additions and 23 deletions

View File

@ -55,7 +55,7 @@ class ManagementErrorEndpointTests {
void errorResponseNeverDetails() {
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(this.errorAttributes, this.errorProperties);
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(new MockHttpServletRequest()));
assertThat(response).containsEntry("message", "");
assertThat(response).doesNotContainKey("message");
assertThat(response).doesNotContainKey("trace");
}
@ -78,7 +78,7 @@ class ManagementErrorEndpointTests {
this.errorProperties.setIncludeMessage(ErrorProperties.IncludeAttribute.ON_PARAM);
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(this.errorAttributes, this.errorProperties);
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(this.request));
assertThat(response).containsEntry("message", "");
assertThat(response).doesNotContainKey("message");
assertThat(response).doesNotContainKey("trace");
}
@ -103,7 +103,7 @@ class ManagementErrorEndpointTests {
this.request.addParameter("message", "false");
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(this.errorAttributes, this.errorProperties);
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(this.request));
assertThat(response).containsEntry("message", "");
assertThat(response).doesNotContainKey("message");
assertThat(response).doesNotContainKey("trace");
}

View File

@ -87,8 +87,8 @@ class DefaultErrorWebExceptionHandlerIntegrationTests {
client.get().uri("/").exchange().expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR).expectBody()
.jsonPath("status").isEqualTo("500").jsonPath("error")
.isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()).jsonPath("path").isEqualTo(("/"))
.jsonPath("message").isEmpty().jsonPath("exception").doesNotExist().jsonPath("trace").doesNotExist()
.jsonPath("requestId").isEqualTo(this.logIdFilter.getLogId());
.jsonPath("message").doesNotExist().jsonPath("exception").doesNotExist().jsonPath("trace")
.doesNotExist().jsonPath("requestId").isEqualTo(this.logIdFilter.getLogId());
assertThat(output).contains("500 Server Error for HTTP GET \"/\"")
.contains("java.lang.IllegalStateException: Expected!");
});
@ -124,7 +124,7 @@ class DefaultErrorWebExceptionHandlerIntegrationTests {
.isBadRequest().expectBody().jsonPath("status").isEqualTo("400").jsonPath("error")
.isEqualTo(HttpStatus.BAD_REQUEST.getReasonPhrase()).jsonPath("path").isEqualTo(("/bind"))
.jsonPath("exception").doesNotExist().jsonPath("errors").doesNotExist().jsonPath("message")
.isEmpty().jsonPath("requestId").isEqualTo(this.logIdFilter.getLogId());
.doesNotExist().jsonPath("requestId").isEqualTo(this.logIdFilter.getLogId());
});
}
@ -227,7 +227,7 @@ class DefaultErrorWebExceptionHandlerIntegrationTests {
.isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR).expectBody().jsonPath("status")
.isEqualTo("500").jsonPath("error")
.isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()).jsonPath("exception")
.isEqualTo(IllegalStateException.class.getName()).jsonPath("message").isEmpty()
.isEqualTo(IllegalStateException.class.getName()).jsonPath("message").doesNotExist()
.jsonPath("requestId").isEqualTo(this.logIdFilter.getLogId());
});
}

View File

@ -88,7 +88,7 @@ class BasicErrorControllerIntegrationTests {
void testErrorForMachineClientDefault() {
load();
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("?trace=true"), Map.class);
assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", null, "", "/");
assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", null, null, "/");
assertThat(entity.getBody()).doesNotContainKey("exception");
assertThat(entity.getBody()).doesNotContainKey("trace");
}
@ -148,7 +148,7 @@ class BasicErrorControllerIntegrationTests {
@SuppressWarnings({ "rawtypes", "unchecked" })
private void exceptionWithoutStackTraceAndMessage(String path) {
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl(path), Map.class);
assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", IllegalStateException.class, "", "/");
assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", IllegalStateException.class, null, "/");
assertThat(entity.getBody()).doesNotContainKey("trace");
}
@ -158,7 +158,7 @@ class BasicErrorControllerIntegrationTests {
load("--server.error.include-exception=true");
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/annotated"), Map.class);
assertErrorAttributes(entity.getBody(), "400", "Bad Request", TestConfiguration.Errors.ExpectedException.class,
"", "/annotated");
null, "/annotated");
}
@Test
@ -176,7 +176,7 @@ class BasicErrorControllerIntegrationTests {
load("--server.error.include-exception=true");
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/annotatedNoReason"), Map.class);
assertErrorAttributes(entity.getBody(), "406", "Not Acceptable",
TestConfiguration.Errors.NoReasonExpectedException.class, "", "/annotatedNoReason");
TestConfiguration.Errors.NoReasonExpectedException.class, null, "/annotatedNoReason");
}
@Test
@ -261,14 +261,14 @@ class BasicErrorControllerIntegrationTests {
@SuppressWarnings({ "rawtypes", "unchecked" })
private void bindingExceptionWithErrors(String param) {
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class);
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, "", "/bind");
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, null, "/bind");
assertThat(entity.getBody()).containsKey("errors");
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void bindingExceptionWithoutErrors(String param) {
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class);
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, "", "/bind");
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, null, "/bind");
assertThat(entity.getBody()).doesNotContainKey("errors");
}
@ -283,7 +283,7 @@ class BasicErrorControllerIntegrationTests {
@SuppressWarnings({ "rawtypes", "unchecked" })
private void bindingExceptionWithoutMessage(String param) {
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(createUrl("/bind" + param), Map.class);
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, "", "/bind");
assertErrorAttributes(entity.getBody(), "400", "Bad Request", BindException.class, null, "/bind");
assertThat(entity.getBody()).doesNotContainKey("errors");
}
@ -294,7 +294,7 @@ class BasicErrorControllerIntegrationTests {
RequestEntity request = RequestEntity.post(URI.create(createUrl("/bodyValidation")))
.accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).body("{}");
ResponseEntity<Map> entity = new TestRestTemplate().exchange(request, Map.class);
assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class, "",
assertErrorAttributes(entity.getBody(), "400", "Bad Request", MethodArgumentNotValidException.class, null,
"/bodyValidation");
assertThat(entity.getBody()).doesNotContainKey("errors");
}

View File

@ -73,7 +73,7 @@ public class DefaultErrorAttributes implements ErrorAttributes {
errorAttributes.remove("trace");
}
if (!options.isIncluded(Include.MESSAGE) && errorAttributes.get("message") != null) {
errorAttributes.put("message", "");
errorAttributes.remove("message");
}
if (!options.isIncluded(Include.BINDING_ERRORS)) {
errorAttributes.remove("errors");

View File

@ -96,7 +96,7 @@ public class DefaultErrorAttributes implements ErrorAttributes, HandlerException
errorAttributes.remove("trace");
}
if (!options.isIncluded(Include.MESSAGE) && errorAttributes.get("message") != null) {
errorAttributes.put("message", "");
errorAttributes.remove("message");
}
if (!options.isIncluded(Include.BINDING_ERRORS)) {
errorAttributes.remove("errors");

View File

@ -93,7 +93,7 @@ class DefaultErrorAttributesTests {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, error),
ErrorAttributeOptions.defaults());
assertThat(attributes.get("error")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.getReasonPhrase());
assertThat(attributes.get("message")).isEqualTo("");
assertThat(attributes).doesNotContainKey("message");
assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value());
}
@ -148,7 +148,7 @@ class DefaultErrorAttributesTests {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(serverRequest,
ErrorAttributeOptions.defaults());
assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error);
assertThat(attributes.get("message")).isEqualTo("");
assertThat(attributes).doesNotContainKey("message");
}
@Test
@ -256,7 +256,7 @@ class DefaultErrorAttributesTests {
MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex),
ErrorAttributeOptions.defaults());
assertThat(attributes).containsEntry("message", "");
assertThat(attributes).doesNotContainKey("message");
assertThat(attributes).doesNotContainKey("errors");
}

View File

@ -113,7 +113,7 @@ class DefaultErrorAttributesTests {
ErrorAttributeOptions.defaults());
assertThat(this.errorAttributes.getError(this.webRequest)).isSameAs(ex);
assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes.get("message").toString()).contains("");
assertThat(attributes).doesNotContainKey("message");
}
@Test
@ -131,7 +131,7 @@ class DefaultErrorAttributesTests {
Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(this.webRequest,
ErrorAttributeOptions.defaults());
assertThat(attributes).doesNotContainKey("exception");
assertThat(attributes.get("message")).asString().contains("");
assertThat(attributes).doesNotContainKey("message");
}
@Test
@ -210,7 +210,7 @@ class DefaultErrorAttributesTests {
.isEqualTo("Validation failed for object='objectName'. Error count: 1");
}
else {
assertThat(attributes.get("message")).isEqualTo("");
assertThat(attributes).doesNotContainKey("message");
}
if (options.isIncluded(Include.BINDING_ERRORS)) {
assertThat(attributes.get("errors")).isEqualTo(bindingResult.getAllErrors());