Compare commits
3 Commits
master
...
feature/ST
| Author | SHA1 | Date |
|---|---|---|
|
|
2ab37c4a68 | |
|
|
93ba1ab526 | |
|
|
fb8afe2b14 |
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
exception.resolver:
|
||||
includeHeaders: false
|
||||
Loading…
Reference in New Issue