Search in sources :

Example 1 with ErrorAttributes

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");
}
Also used : ErrorAttributes(org.springframework.boot.web.servlet.error.ErrorAttributes) DefaultErrorAttributes(org.springframework.boot.web.servlet.error.DefaultErrorAttributes) WebRequest(org.springframework.web.context.request.WebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ErrorAttributeOptions(org.springframework.boot.web.error.ErrorAttributeOptions) DefaultErrorAttributes(org.springframework.boot.web.servlet.error.DefaultErrorAttributes) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 2 with ErrorAttributes

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"));
}
Also used : ErrorAttributes(org.springframework.boot.web.servlet.error.ErrorAttributes) DefaultErrorAttributes(org.springframework.boot.web.servlet.error.DefaultErrorAttributes) WebRequest(org.springframework.web.context.request.WebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ErrorAttributeOptions(org.springframework.boot.web.error.ErrorAttributeOptions) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 3 with ErrorAttributes

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");
    });
}
Also used : ErrorAttributes(org.springframework.boot.web.servlet.error.ErrorAttributes) DispatcherServletWebRequest(org.springframework.web.servlet.handler.DispatcherServletWebRequest) View(org.springframework.web.servlet.View) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 4 with ErrorAttributes

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");
    });
}
Also used : ErrorAttributes(org.springframework.boot.web.servlet.error.ErrorAttributes) DispatcherServletWebRequest(org.springframework.web.servlet.handler.DispatcherServletWebRequest) View(org.springframework.web.servlet.View) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 5 with ErrorAttributes

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.");
    });
}
Also used : ErrorAttributes(org.springframework.boot.web.servlet.error.ErrorAttributes) DispatcherServletWebRequest(org.springframework.web.servlet.handler.DispatcherServletWebRequest) View(org.springframework.web.servlet.View) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)6 ErrorAttributes (org.springframework.boot.web.servlet.error.ErrorAttributes)6 ErrorAttributeOptions (org.springframework.boot.web.error.ErrorAttributeOptions)3 DefaultErrorAttributes (org.springframework.boot.web.servlet.error.DefaultErrorAttributes)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 WebRequest (org.springframework.web.context.request.WebRequest)3 View (org.springframework.web.servlet.View)3 DispatcherServletWebRequest (org.springframework.web.servlet.handler.DispatcherServletWebRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2