Search in sources :

Example 1 with DefaultErrorAttributes

use of org.springframework.boot.web.servlet.error.DefaultErrorAttributes 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 DefaultErrorAttributes

use of org.springframework.boot.web.servlet.error.DefaultErrorAttributes in project spring-boot by spring-projects.

the class CompositeHandlerExceptionResolver method extractResolvers.

private List<HandlerExceptionResolver> extractResolvers() {
    List<HandlerExceptionResolver> list = new ArrayList<>(this.beanFactory.getBeansOfType(HandlerExceptionResolver.class).values());
    list.remove(this);
    AnnotationAwareOrderComparator.sort(list);
    if (list.isEmpty()) {
        list.add(new DefaultErrorAttributes());
        list.add(new DefaultHandlerExceptionResolver());
    }
    return list;
}
Also used : HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) ArrayList(java.util.ArrayList) DefaultErrorAttributes(org.springframework.boot.web.servlet.error.DefaultErrorAttributes) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver)

Example 3 with DefaultErrorAttributes

use of org.springframework.boot.web.servlet.error.DefaultErrorAttributes in project spring-boot by spring-projects.

the class ManagementErrorEndpointTests method errorResponseWithDefaultErrorAttributesSubclassWithoutDelegation.

@Test
void errorResponseWithDefaultErrorAttributesSubclassWithoutDelegation() {
    ErrorAttributes attributes = new DefaultErrorAttributes() {

        @Override
        public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
            return Collections.singletonMap("error", "custom error");
        }
    };
    ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(attributes, this.errorProperties);
    Map<String, Object> response = endpoint.invoke(new ServletWebRequest(new MockHttpServletRequest()));
    assertThat(response).containsExactly(entry("error", "custom error"));
}
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)

Aggregations

DefaultErrorAttributes (org.springframework.boot.web.servlet.error.DefaultErrorAttributes)3 Test (org.junit.jupiter.api.Test)2 ErrorAttributeOptions (org.springframework.boot.web.error.ErrorAttributeOptions)2 ErrorAttributes (org.springframework.boot.web.servlet.error.ErrorAttributes)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)2 WebRequest (org.springframework.web.context.request.WebRequest)2 ArrayList (java.util.ArrayList)1 HandlerExceptionResolver (org.springframework.web.servlet.HandlerExceptionResolver)1 DefaultHandlerExceptionResolver (org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver)1