From b6a81c9e8ecf76dd6b7061136b4383c03087db60 Mon Sep 17 00:00:00 2001 From: stephano-tri Date: Tue, 30 Jan 2024 23:31:00 +0900 Subject: [PATCH] FEATURE: implementation custom error handler #4 --- .../improve/kafkaboot/common/ErrorResponse.kt | 21 +++++++++++ .../GlobalExceptionAttributesConfig.kt | 7 ---- .../controller/GlobalExceptionHandler.kt | 35 +++++++++++++++++++ .../exception/BadRequestException.kt | 7 ++++ 4 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/eom/improve/kafkaboot/common/ErrorResponse.kt delete mode 100644 src/main/kotlin/eom/improve/kafkaboot/configuration/GlobalExceptionAttributesConfig.kt create mode 100644 src/main/kotlin/eom/improve/kafkaboot/controller/GlobalExceptionHandler.kt create mode 100644 src/main/kotlin/eom/improve/kafkaboot/exception/BadRequestException.kt diff --git a/src/main/kotlin/eom/improve/kafkaboot/common/ErrorResponse.kt b/src/main/kotlin/eom/improve/kafkaboot/common/ErrorResponse.kt new file mode 100644 index 0000000..6e855e8 --- /dev/null +++ b/src/main/kotlin/eom/improve/kafkaboot/common/ErrorResponse.kt @@ -0,0 +1,21 @@ +package eom.improve.kafkaboot.common + +import com.fasterxml.jackson.annotation.JsonProperty +import org.springframework.http.HttpStatus +import java.time.ZonedDateTime + +data class ErrorResponse( + @get:JsonProperty("path") val path: String, + @get:JsonProperty("status") val status: HttpStatus, + @get:JsonProperty("message") val message: String, + @get:JsonProperty("error") val error: String = status.reasonPhrase, + @get:JsonProperty("requestId") val requestId: String = path.substringAfterLast("/"), + @get:JsonProperty("timestamp") val timestamp: ZonedDateTime= ZonedDateTime.now() +) { + constructor() : this( + path = "", + status = HttpStatus.INTERNAL_SERVER_ERROR, + message = HttpStatus.INTERNAL_SERVER_ERROR.name, + timestamp = ZonedDateTime.now() + ) +} diff --git a/src/main/kotlin/eom/improve/kafkaboot/configuration/GlobalExceptionAttributesConfig.kt b/src/main/kotlin/eom/improve/kafkaboot/configuration/GlobalExceptionAttributesConfig.kt deleted file mode 100644 index 08065cf..0000000 --- a/src/main/kotlin/eom/improve/kafkaboot/configuration/GlobalExceptionAttributesConfig.kt +++ /dev/null @@ -1,7 +0,0 @@ -package eom.improve.kafkaboot.configuration - -import org.springframework.boot.web.reactive.error.DefaultErrorAttributes - -class GlobalExceptionAttributesConfig : DefaultErrorAttributes { - -} \ No newline at end of file diff --git a/src/main/kotlin/eom/improve/kafkaboot/controller/GlobalExceptionHandler.kt b/src/main/kotlin/eom/improve/kafkaboot/controller/GlobalExceptionHandler.kt new file mode 100644 index 0000000..2d4b8a8 --- /dev/null +++ b/src/main/kotlin/eom/improve/kafkaboot/controller/GlobalExceptionHandler.kt @@ -0,0 +1,35 @@ +package eom.improve.kafkaboot.controller + +import eom.improve.kafkaboot.common.ErrorResponse +import eom.improve.kafkaboot.exception.BadRequestException +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.bind.annotation.ResponseBody +import org.springframework.web.bind.annotation.RestControllerAdvice +import org.springframework.web.bind.support.WebExchangeBindException +import org.springframework.web.server.ServerWebExchange +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono +import reactor.kotlin.core.publisher.toMono +import java.util.stream.Collectors + +@RestControllerAdvice +class GlobalExceptionHandler { + + @ExceptionHandler(WebExchangeBindException::class) + @ResponseBody + fun handleBadRequestException(exchange: ServerWebExchange, ex: WebExchangeBindException): Mono { + var defaultMsg: String = ex.bindingResult.fieldErrors.stream() + .map { fieldError -> fieldError.defaultMessage } + .collect(Collectors.joining(", ")) + + return ErrorResponse( + path = exchange.request.path.toString(), + status = HttpStatus.BAD_REQUEST, + message = defaultMsg, + error = HttpStatus.BAD_REQUEST.reasonPhrase, + requestId = exchange.request.id + ).toMono() + } + // need to implement error logging publish to kafka +} diff --git a/src/main/kotlin/eom/improve/kafkaboot/exception/BadRequestException.kt b/src/main/kotlin/eom/improve/kafkaboot/exception/BadRequestException.kt new file mode 100644 index 0000000..27bfd8f --- /dev/null +++ b/src/main/kotlin/eom/improve/kafkaboot/exception/BadRequestException.kt @@ -0,0 +1,7 @@ +package eom.improve.kafkaboot.exception + +class BadRequestException : RuntimeException { + constructor(message : String) : super(message) + constructor(message : String, cause : Throwable) : super(message, cause) + constructor(cause : Throwable) : super(cause) +}