diff --git a/exception-handler-spring-web/src/main/kotlin/ru/touchin/exception/handler/spring/advices/ExceptionHandlerAdvice.kt b/exception-handler-spring-web/src/main/kotlin/ru/touchin/exception/handler/spring/advices/ExceptionHandlerAdvice.kt index 9f050d8..7e8a6b3 100644 --- a/exception-handler-spring-web/src/main/kotlin/ru/touchin/exception/handler/spring/advices/ExceptionHandlerAdvice.kt +++ b/exception-handler-spring-web/src/main/kotlin/ru/touchin/exception/handler/spring/advices/ExceptionHandlerAdvice.kt @@ -1,11 +1,13 @@ package ru.touchin.exception.handler.spring.advices +import org.springframework.http.HttpHeaders import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.ExceptionHandler import org.springframework.web.bind.annotation.RestControllerAdvice import ru.touchin.exception.handler.dto.ExceptionResolverResult import ru.touchin.exception.handler.spring.creators.ExceptionResponseBodyCreator import ru.touchin.exception.handler.spring.logger.Logger +import ru.touchin.exception.handler.spring.properties.ExceptionResolverProperties import ru.touchin.exception.handler.spring.resolvers.ExceptionResolver @RestControllerAdvice @@ -13,6 +15,7 @@ class ExceptionHandlerAdvice( exceptionResolversList: List, private val logger: Logger, private val exceptionResponseBodyCreator: ExceptionResponseBodyCreator, + private val exceptionResolverProperties: ExceptionResolverProperties, ) { private val exceptionResolvers = exceptionResolversList.asSequence() @@ -30,7 +33,12 @@ class ExceptionHandlerAdvice( val body = exceptionResponseBodyCreator(result.apiError) - return ResponseEntity(body, result.status) + val headers = if (exceptionResolverProperties.includeHeaders) HttpHeaders().apply { + set("X-Error-Code", result.apiError.errorCode.toString()) + set("X-Error-Message", result.apiError.errorMessage) + } else null + + return ResponseEntity(body, headers, result.status) } } diff --git a/exception-handler-spring-web/src/main/kotlin/ru/touchin/exception/handler/spring/configurations/ExceptionHandlerConfiguration.kt b/exception-handler-spring-web/src/main/kotlin/ru/touchin/exception/handler/spring/configurations/ExceptionHandlerConfiguration.kt index 9aa4c2f..e06cede 100644 --- a/exception-handler-spring-web/src/main/kotlin/ru/touchin/exception/handler/spring/configurations/ExceptionHandlerConfiguration.kt +++ b/exception-handler-spring-web/src/main/kotlin/ru/touchin/exception/handler/spring/configurations/ExceptionHandlerConfiguration.kt @@ -1,6 +1,7 @@ package ru.touchin.exception.handler.spring.configurations import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean +import org.springframework.boot.context.properties.ConfigurationPropertiesScan import org.springframework.context.annotation.Bean import org.springframework.context.annotation.ComponentScan import org.springframework.context.annotation.Configuration @@ -14,6 +15,7 @@ import ru.touchin.exception.handler.spring.logger.Logger "ru.touchin.exception.handler.spring.advices", "ru.touchin.exception.handler.spring.resolvers", ) +@ConfigurationPropertiesScan("ru.touchin.exception.handler.spring") class ExceptionHandlerConfiguration { @Bean diff --git a/exception-handler-spring-web/src/main/kotlin/ru/touchin/exception/handler/spring/properties/ExceptionResolverProperties.kt b/exception-handler-spring-web/src/main/kotlin/ru/touchin/exception/handler/spring/properties/ExceptionResolverProperties.kt new file mode 100644 index 0000000..5a7e605 --- /dev/null +++ b/exception-handler-spring-web/src/main/kotlin/ru/touchin/exception/handler/spring/properties/ExceptionResolverProperties.kt @@ -0,0 +1,10 @@ +package ru.touchin.exception.handler.spring.properties + +import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.boot.context.properties.ConstructorBinding + +@ConstructorBinding +@ConfigurationProperties(prefix = "exception.resolver") +data class ExceptionResolverProperties( + val includeHeaders: Boolean = false, +) diff --git a/exception-handler-spring-web/src/test/kotlin/ru/touchin/exception/handler/spring/advices/ExceptionHandlerAdviceMvcTest.kt b/exception-handler-spring-web/src/test/kotlin/ru/touchin/exception/handler/spring/advices/ExceptionHandlerAdviceMvcTest.kt index 523831f..849c4fb 100644 --- a/exception-handler-spring-web/src/test/kotlin/ru/touchin/exception/handler/spring/advices/ExceptionHandlerAdviceMvcTest.kt +++ b/exception-handler-spring-web/src/test/kotlin/ru/touchin/exception/handler/spring/advices/ExceptionHandlerAdviceMvcTest.kt @@ -26,6 +26,7 @@ import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import ru.touchin.exception.handler.spring.creators.ExceptionResponseBodyCreator import ru.touchin.exception.handler.spring.logger.Logger +import ru.touchin.exception.handler.spring.properties.ExceptionResolverProperties import ru.touchin.exception.handler.spring.resolvers.FallbackExceptionResolver import ru.touchin.exception.handler.spring.resolvers.IllegalStateExceptionResolver1 import ru.touchin.exception.handler.spring.resolvers.IllegalStateExceptionResolver2 @@ -65,14 +66,14 @@ internal class ExceptionHandlerAdviceMvcTest { } @Test - @DisplayName("Должна вернуться ошибка InternalServerError с кодом -1") + @DisplayName("Должна вернуться ошибка InternalServerError с кодом -2") fun shouldGetInternalServerError() { mockMvc .perform(get("/api/errors/runtime")) .andDo(print()) .andExpect(status().isInternalServerError) .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) - .andExpect(jsonPath("$.errorCode", `is`(-1))) + .andExpect(jsonPath("$.errorCode", `is`(-2))) .andExpect(jsonPath("$.errorMessage", `is`("my runtime error"))) } @@ -101,7 +102,8 @@ internal class ExceptionHandlerAdviceMvcTest { val exceptionHandlerAdvice = ExceptionHandlerAdvice( exceptionResolversList = resolvers, exceptionResponseBodyCreator = exceptionResponseBodyCreator, - logger = logger + logger = logger, + exceptionResolverProperties = ExceptionResolverProperties() ) exceptionHandlerAdvice.handleException(IllegalStateException("error")) diff --git a/exception-handler-spring-web/src/test/kotlin/ru/touchin/exception/handler/spring/advices/ExceptionHandlerAdviceTest.kt b/exception-handler-spring-web/src/test/kotlin/ru/touchin/exception/handler/spring/advices/ExceptionHandlerAdviceTest.kt index 7396a53..668d2d0 100644 --- a/exception-handler-spring-web/src/test/kotlin/ru/touchin/exception/handler/spring/advices/ExceptionHandlerAdviceTest.kt +++ b/exception-handler-spring-web/src/test/kotlin/ru/touchin/exception/handler/spring/advices/ExceptionHandlerAdviceTest.kt @@ -14,6 +14,7 @@ import org.springframework.boot.test.context.SpringBootTest import org.springframework.test.context.ActiveProfiles import ru.touchin.exception.handler.spring.creators.ExceptionResponseBodyCreator import ru.touchin.exception.handler.spring.logger.Logger +import ru.touchin.exception.handler.spring.properties.ExceptionResolverProperties import ru.touchin.exception.handler.spring.resolvers.FallbackExceptionResolver import ru.touchin.exception.handler.spring.resolvers.IllegalStateExceptionResolver1 import ru.touchin.exception.handler.spring.resolvers.IllegalStateExceptionResolver2 @@ -44,6 +45,7 @@ internal class ExceptionHandlerAdviceTest { exceptionResolversList = resolvers, exceptionResponseBodyCreator = exceptionResponseBodyCreator, logger = logger, + exceptionResolverProperties = ExceptionResolverProperties() ) } @@ -77,6 +79,7 @@ internal class ExceptionHandlerAdviceTest { exceptionResolversList = resolvers, exceptionResponseBodyCreator = exceptionResponseBodyCreator, logger = logger, + exceptionResolverProperties = ExceptionResolverProperties() ) exceptionHandlerAdvice.handleException(RuntimeException("error"))