Added httpHeaders in ExceptionHandlerAdvice (#70)
This commit is contained in:
parent
345db02feb
commit
be1ef45fb2
|
|
@ -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,
|
||||
)
|
||||
|
|
@ -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"))
|
||||
|
|
|
|||
|
|
@ -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"))
|
||||
|
|
|
|||
Loading…
Reference in New Issue