Compare commits

...

3 Commits

Author SHA1 Message Date
Denis Kazantsev 2ab37c4a68 Added property in ExceptionHandlerAdvice 2022-03-30 13:31:42 +03:00
Denis Kazantsev 93ba1ab526 Added includeHeaders flag in ExceptionResolverResult 2022-03-30 12:42:45 +03:00
Denis Kazantsev fb8afe2b14 Added httpHeaders in ExceptionHandlerAdvice 2022-03-30 11:09:36 +03:00
4 changed files with 23 additions and 1 deletions

View File

@ -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<ExceptionResolver>,
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)
}
}

View File

@ -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

View File

@ -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,
)

View File

@ -0,0 +1,2 @@
exception.resolver:
includeHeaders: false