diff --git a/tosspay-mock/src/main/kotlin/com/sangdol/tosspaymock/exception/TosspayExceptionHandler.kt b/tosspay-mock/src/main/kotlin/com/sangdol/tosspaymock/exception/TosspayExceptionHandler.kt new file mode 100644 index 00000000..44fdbb80 --- /dev/null +++ b/tosspay-mock/src/main/kotlin/com/sangdol/tosspaymock/exception/TosspayExceptionHandler.kt @@ -0,0 +1,29 @@ +package com.sangdol.tosspaymock.exception + +import com.sangdol.common.types.web.CommonErrorResponse +import io.github.oshai.kotlinlogging.KLogger +import io.github.oshai.kotlinlogging.KotlinLogging +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.bind.annotation.RestControllerAdvice + +private val log: KLogger = KotlinLogging.logger {} + +@RestControllerAdvice +class TosspayExceptionHandler { + + @ExceptionHandler(value = [TosspayException::class]) + fun handleTosspayException( + e: TosspayException + ): ResponseEntity { + val code = e.errorCode + val name = (code as Enum<*>).name + val message = e.message + + log.warn { "[TosspayExceptionHandler] 결제 처리 과정 중 오류 발생: error=${name}, message=$message" } + + return ResponseEntity + .status(code.httpStatus.value()) + .body(CommonErrorResponse(name, message)) + } +}