use of org.springframework.boot.web.servlet.error.ErrorAttributes in project spring-boot by spring-projects.
the class ManagementErrorEndpointTests method errorResponseWithDefaultErrorAttributesSubclassUsingDelegation.
@Test
void errorResponseWithDefaultErrorAttributesSubclassUsingDelegation() {
ErrorAttributes attributes = new DefaultErrorAttributes() {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
Map<String, Object> response = super.getErrorAttributes(webRequest, options);
response.put("error", "custom error");
response.put("custom", "value");
response.remove("path");
return response;
}
};
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(attributes, this.errorProperties);
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(new MockHttpServletRequest()));
assertThat(response).containsEntry("error", "custom error");
assertThat(response).containsEntry("custom", "value");
assertThat(response).doesNotContainKey("path");
assertThat(response).containsKey("timestamp");
}
use of org.springframework.boot.web.servlet.error.ErrorAttributes in project spring-boot by spring-projects.
the class ManagementErrorEndpointTests method errorResponseWithCustomErrorAttributesUsingDeprecatedApi.
@Test
void errorResponseWithCustomErrorAttributesUsingDeprecatedApi() {
ErrorAttributes attributes = new ErrorAttributes() {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
return Collections.singletonMap("message", "An error occurred");
}
@Override
public Throwable getError(WebRequest webRequest) {
return null;
}
};
ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(attributes, this.errorProperties);
Map<String, Object> response = endpoint.invoke(new ServletWebRequest(new MockHttpServletRequest()));
assertThat(response).containsExactly(entry("message", "An error occurred"));
}
use of org.springframework.boot.web.servlet.error.ErrorAttributes in project spring-boot by spring-projects.
the class ErrorMvcAutoConfigurationTests method renderContainsViewWithExceptionDetails.
@Test
void renderContainsViewWithExceptionDetails() {
this.contextRunner.run((context) -> {
View errorView = context.getBean("error", View.class);
ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"), false);
errorView.render(errorAttributes.getErrorAttributes(webRequest, withAllOptions()), webRequest.getRequest(), webRequest.getResponse());
assertThat(webRequest.getResponse().getContentType()).isEqualTo("text/html;charset=UTF-8");
String responseString = ((MockHttpServletResponse) webRequest.getResponse()).getContentAsString();
assertThat(responseString).contains("<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>").contains("<div>Exception message</div>").contains("<div style='white-space:pre-wrap;'>java.lang.IllegalStateException");
});
}
use of org.springframework.boot.web.servlet.error.ErrorAttributes in project spring-boot by spring-projects.
the class ErrorMvcAutoConfigurationTests method renderCanUseJavaTimeTypeAsTimestamp.
@Test
void renderCanUseJavaTimeTypeAsTimestamp() {
// gh-23256
this.contextRunner.run((context) -> {
View errorView = context.getBean("error", View.class);
ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"), false);
Map<String, Object> attributes = errorAttributes.getErrorAttributes(webRequest, withAllOptions());
attributes.put("timestamp", Clock.systemUTC().instant());
errorView.render(attributes, webRequest.getRequest(), webRequest.getResponse());
assertThat(webRequest.getResponse().getContentType()).isEqualTo("text/html;charset=UTF-8");
String responseString = ((MockHttpServletResponse) webRequest.getResponse()).getContentAsString();
assertThat(responseString).contains("This application has no explicit mapping for /error");
});
}
use of org.springframework.boot.web.servlet.error.ErrorAttributes in project spring-boot by spring-projects.
the class ErrorMvcAutoConfigurationTests method renderWhenAlreadyCommittedLogsMessage.
@Test
void renderWhenAlreadyCommittedLogsMessage(CapturedOutput output) {
this.contextRunner.run((context) -> {
View errorView = context.getBean("error", View.class);
ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class);
DispatcherServletWebRequest webRequest = createWebRequest(new IllegalStateException("Exception message"), true);
errorView.render(errorAttributes.getErrorAttributes(webRequest, withAllOptions()), webRequest.getRequest(), webRequest.getResponse());
assertThat(output).contains("Cannot render error page for request [/path] " + "and exception [Exception message] as the response has " + "already been committed. As a result, the response may have the wrong status code.");
});
}
Aggregations