-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: implementation custom error handler #4
- Loading branch information
1 parent
0ab49f3
commit b6a81c9
Showing
4 changed files
with
63 additions
and
7 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/eom/improve/kafkaboot/common/ErrorResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
) | ||
} |
7 changes: 0 additions & 7 deletions
7
src/main/kotlin/eom/improve/kafkaboot/configuration/GlobalExceptionAttributesConfig.kt
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/eom/improve/kafkaboot/controller/GlobalExceptionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ErrorResponse> { | ||
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 | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/eom/improve/kafkaboot/exception/BadRequestException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |