From f98609eb74ab0bfe7a15849cd4b835b5a0acf400 Mon Sep 17 00:00:00 2001 From: jaeyeonkim Date: Sat, 24 Aug 2024 19:00:45 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20GlobalExceptionHandler=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wespot/common/GlobalExceptionHandler.kt | 10 +++++----- .../common/util/ReasonPhraseUtilTest.kt | 1 + .../com/wespot/exception/ExceptionResponse.kt | 20 ++++++++++++++++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/wespot/common/GlobalExceptionHandler.kt b/app/src/main/kotlin/com/wespot/common/GlobalExceptionHandler.kt index 55e66710..05ff8ba1 100644 --- a/app/src/main/kotlin/com/wespot/common/GlobalExceptionHandler.kt +++ b/app/src/main/kotlin/com/wespot/common/GlobalExceptionHandler.kt @@ -44,14 +44,14 @@ class GlobalExceptionHandler( } return ResponseEntity.status(exception.status) - .body(ExceptionResponse(exception.view, problemDetail)) + .body(ExceptionResponse.of(exception.view, problemDetail)) } @ExceptionHandler(Exception::class) fun handleException( exception: Exception, request: HttpServletRequest - ): ResponseEntity { + ): ResponseEntity { notifyException(true, request, exception) val internalErrorMessage = "서버에서 알 수 없는 에러가 발생했습니다." logger.error(internalErrorMessage, exception) @@ -65,7 +65,7 @@ class GlobalExceptionHandler( } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .body(problemDetail) + .body(ExceptionResponse.of(ExceptionView.DIALOG, problemDetail)) } @ExceptionHandler(FeignException::class) @@ -85,7 +85,7 @@ class GlobalExceptionHandler( } return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body(ExceptionResponse(ExceptionView.TOAST, problemDetail)) + .body(ExceptionResponse.of(ExceptionView.TOAST, problemDetail)) } override fun handleMethodArgumentNotValid( @@ -105,7 +105,7 @@ class GlobalExceptionHandler( } return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body(ExceptionResponse(ExceptionView.TOAST, problemDetail)) + .body(ExceptionResponse.of(ExceptionView.TOAST, problemDetail)) } private fun notifyException(isError: Boolean, request: HttpServletRequest, exception: Exception) { diff --git a/app/src/test/kotlin/com/wespot/common/util/ReasonPhraseUtilTest.kt b/app/src/test/kotlin/com/wespot/common/util/ReasonPhraseUtilTest.kt index c09f3233..133aced1 100644 --- a/app/src/test/kotlin/com/wespot/common/util/ReasonPhraseUtilTest.kt +++ b/app/src/test/kotlin/com/wespot/common/util/ReasonPhraseUtilTest.kt @@ -5,6 +5,7 @@ import io.kotest.matchers.shouldBe import org.junit.jupiter.api.Test import org.springframework.http.HttpStatus import java.net.URI +import kotlin.math.exp class ReasonPhraseUtilTest { diff --git a/common/src/main/kotlin/com/wespot/exception/ExceptionResponse.kt b/common/src/main/kotlin/com/wespot/exception/ExceptionResponse.kt index 26af20ec..db762cf9 100644 --- a/common/src/main/kotlin/com/wespot/exception/ExceptionResponse.kt +++ b/common/src/main/kotlin/com/wespot/exception/ExceptionResponse.kt @@ -4,6 +4,24 @@ import org.springframework.http.ProblemDetail class ExceptionResponse( val view: ExceptionView, - val problemDetail: ProblemDetail + val type: String, + val title: String, + val status: Int, + val detail: String, + val instance: String, ) { + companion object { + + fun of(view: ExceptionView, problemDetail: ProblemDetail): ExceptionResponse { + return ExceptionResponse( + view = view, + type = problemDetail.type.path, + title = problemDetail.title!!, + status = problemDetail.status, + detail = problemDetail.detail!!, + instance = problemDetail.instance!!.path, + ) + } + + } }