Skip to content

Commit

Permalink
refactor: GlobalExceptionHandler 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kpeel5839 committed Aug 24, 2024
1 parent 2f15956 commit f98609e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
10 changes: 5 additions & 5 deletions app/src/main/kotlin/com/wespot/common/GlobalExceptionHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProblemDetail> {
): ResponseEntity<ExceptionResponse> {
notifyException(true, request, exception)
val internalErrorMessage = "서버에서 알 수 없는 에러가 발생했습니다."
logger.error(internalErrorMessage, exception)
Expand All @@ -65,7 +65,7 @@ class GlobalExceptionHandler(
}

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(problemDetail)
.body(ExceptionResponse.of(ExceptionView.DIALOG, problemDetail))
}

@ExceptionHandler(FeignException::class)
Expand All @@ -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(
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
20 changes: 19 additions & 1 deletion common/src/main/kotlin/com/wespot/exception/ExceptionResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

}
}

0 comments on commit f98609e

Please sign in to comment.