Search in sources :

Example 1 with ErrorAttributeOptions

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

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

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

the class ErrorAttributesOptionsTests method excludingFromEmptyAttributesReturnEmptyList.

@Test
void excludingFromEmptyAttributesReturnEmptyList() {
    ErrorAttributeOptions options = ErrorAttributeOptions.of(EnumSet.noneOf(Include.class));
    assertThat(options.excluding(Include.EXCEPTION).getIncludes()).isEmpty();
}
Also used : Include(org.springframework.boot.web.error.ErrorAttributeOptions.Include) ErrorAttributeOptions(org.springframework.boot.web.error.ErrorAttributeOptions) Test(org.junit.jupiter.api.Test)

Example 4 with ErrorAttributeOptions

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

the class ErrorAttributesOptionsTests method excludingFromMatchingAttributesRemoveMatch.

@Test
void excludingFromMatchingAttributesRemoveMatch() {
    ErrorAttributeOptions options = ErrorAttributeOptions.of(EnumSet.of(Include.EXCEPTION, Include.STACK_TRACE));
    assertThat(options.excluding(Include.EXCEPTION).getIncludes()).containsOnly(Include.STACK_TRACE);
}
Also used : ErrorAttributeOptions(org.springframework.boot.web.error.ErrorAttributeOptions) Test(org.junit.jupiter.api.Test)

Example 5 with ErrorAttributeOptions

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

the class ErrorAttributesOptionsTests method includingFromMatchingAttributesDoesNotModifyOptions.

@Test
void includingFromMatchingAttributesDoesNotModifyOptions() {
    ErrorAttributeOptions options = ErrorAttributeOptions.of(EnumSet.of(Include.EXCEPTION, Include.STACK_TRACE));
    assertThat(options.including(Include.EXCEPTION).getIncludes()).containsOnly(Include.EXCEPTION, Include.STACK_TRACE);
}
Also used : ErrorAttributeOptions(org.springframework.boot.web.error.ErrorAttributeOptions) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)7 ErrorAttributeOptions (org.springframework.boot.web.error.ErrorAttributeOptions)7 DefaultErrorAttributes (org.springframework.boot.web.servlet.error.DefaultErrorAttributes)3 ErrorAttributes (org.springframework.boot.web.servlet.error.ErrorAttributes)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 WebRequest (org.springframework.web.context.request.WebRequest)3 Include (org.springframework.boot.web.error.ErrorAttributeOptions.Include)2