generated from pricelees/issue-pr-template
refactor: ExceptionHandler에 새로운 예외 응답 타입 및 ResponseEntity 적용
This commit is contained in:
parent
0cc69179d1
commit
790fe420a0
@ -10,5 +10,5 @@ data class CommonApiResponse<T>(
|
||||
|
||||
data class CommonErrorResponse(
|
||||
val errorType: ErrorType,
|
||||
val message: String = errorType.description
|
||||
val message: String? = errorType.description
|
||||
)
|
||||
|
||||
@ -2,16 +2,15 @@ package roomescape.common.exception
|
||||
|
||||
import io.github.oshai.kotlinlogging.KLogger
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import jakarta.servlet.http.HttpServletResponse
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler
|
||||
import org.springframework.web.bind.annotation.ResponseStatus
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice
|
||||
import org.springframework.web.client.ResourceAccessException
|
||||
import roomescape.common.dto.response.RoomescapeErrorResponse
|
||||
import roomescape.common.dto.response.CommonErrorResponse
|
||||
|
||||
@RestControllerAdvice
|
||||
class ExceptionControllerAdvice(
|
||||
@ -19,57 +18,59 @@ class ExceptionControllerAdvice(
|
||||
) {
|
||||
|
||||
@ExceptionHandler(value = [RoomescapeException::class])
|
||||
fun handleRoomEscapeException(
|
||||
e: RoomescapeException,
|
||||
response: HttpServletResponse
|
||||
): RoomescapeErrorResponse {
|
||||
fun handleRoomEscapeException(e: RoomescapeException): ResponseEntity<CommonErrorResponse> {
|
||||
logger.error(e) { "message: ${e.message}, invalidValue: ${e.invalidValue}" }
|
||||
response.status = e.httpStatus.value()
|
||||
|
||||
return RoomescapeErrorResponse.of(e.errorType, e.message!!)
|
||||
return ResponseEntity
|
||||
.status(e.httpStatus)
|
||||
.body(CommonErrorResponse(e.errorType))
|
||||
}
|
||||
|
||||
@ExceptionHandler(ResourceAccessException::class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
fun handleResourceAccessException(e: ResourceAccessException): RoomescapeErrorResponse {
|
||||
fun handleResourceAccessException(e: ResourceAccessException): ResponseEntity<CommonErrorResponse> {
|
||||
logger.error(e) { "message: ${e.message}" }
|
||||
|
||||
return RoomescapeErrorResponse.of(ErrorType.PAYMENT_SERVER_ERROR, ErrorType.PAYMENT_SERVER_ERROR.description)
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(CommonErrorResponse(ErrorType.PAYMENT_SERVER_ERROR))
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = [HttpMessageNotReadableException::class])
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
fun handleHttpMessageNotReadableException(e: HttpMessageNotReadableException): RoomescapeErrorResponse {
|
||||
fun handleHttpMessageNotReadableException(e: HttpMessageNotReadableException): ResponseEntity<CommonErrorResponse> {
|
||||
logger.error(e) { "message: ${e.message}" }
|
||||
|
||||
return RoomescapeErrorResponse.of(ErrorType.INVALID_REQUEST_DATA_TYPE,
|
||||
ErrorType.INVALID_REQUEST_DATA_TYPE.description)
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.body(CommonErrorResponse(ErrorType.INVALID_REQUEST_DATA_TYPE))
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = [MethodArgumentNotValidException::class])
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
fun handleMethodArgumentNotValidException(e: MethodArgumentNotValidException): RoomescapeErrorResponse {
|
||||
fun handleMethodArgumentNotValidException(e: MethodArgumentNotValidException): ResponseEntity<CommonErrorResponse> {
|
||||
val messages: String = e.bindingResult.allErrors
|
||||
.mapNotNull { it.defaultMessage }
|
||||
.joinToString(", ")
|
||||
logger.error(e) { "message: $messages" }
|
||||
|
||||
return RoomescapeErrorResponse.of(ErrorType.INVALID_REQUEST_DATA, messages)
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.body(CommonErrorResponse(ErrorType.INVALID_REQUEST_DATA, messages))
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = [HttpRequestMethodNotSupportedException::class])
|
||||
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
|
||||
fun handleHttpRequestMethodNotSupportedException(e: HttpRequestMethodNotSupportedException): RoomescapeErrorResponse {
|
||||
fun handleHttpRequestMethodNotSupportedException(e: HttpRequestMethodNotSupportedException): ResponseEntity<CommonErrorResponse> {
|
||||
logger.error(e) { "message: ${e.message}" }
|
||||
|
||||
return RoomescapeErrorResponse.of(ErrorType.METHOD_NOT_ALLOWED, ErrorType.METHOD_NOT_ALLOWED.description)
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.METHOD_NOT_ALLOWED)
|
||||
.body(CommonErrorResponse(ErrorType.METHOD_NOT_ALLOWED))
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = [Exception::class])
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
fun handleException(e: Exception): RoomescapeErrorResponse {
|
||||
fun handleException(e: Exception): ResponseEntity<CommonErrorResponse> {
|
||||
logger.error(e) { "message: ${e.message}" }
|
||||
|
||||
return RoomescapeErrorResponse.of(ErrorType.UNEXPECTED_ERROR, ErrorType.UNEXPECTED_ERROR.description)
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(CommonErrorResponse(ErrorType.UNEXPECTED_ERROR))
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user