Skip to content

Commit

Permalink
FEATURE: implementation custom error handler #4
Browse files Browse the repository at this point in the history
  • Loading branch information
stephano-tri committed Jan 30, 2024
1 parent 0ab49f3 commit b6a81c9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
21 changes: 21 additions & 0 deletions src/main/kotlin/eom/improve/kafkaboot/common/ErrorResponse.kt
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()
)
}

This file was deleted.

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
}
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)
}

0 comments on commit b6a81c9

Please sign in to comment.