refactor: ExceptionHandler에서 API 에러 응답을 로깅하도록 수정

This commit is contained in:
이상진 2025-07-29 14:04:35 +09:00
parent 7e0bfd9f92
commit 5e0a52cb47

View File

@ -2,55 +2,110 @@ package roomescape.common.exception
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging
import org.slf4j.MDC
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice
import roomescape.auth.exception.AuthException
import roomescape.common.dto.response.CommonErrorResponse
import roomescape.common.log.ApiLogMessageConverter
import roomescape.common.log.ConvertResponseMessageRequest
import roomescape.common.log.LogType
private val log: KLogger = KotlinLogging.logger {}
@RestControllerAdvice
class ExceptionControllerAdvice(
private val log: KLogger = KotlinLogging.logger {}
private val messageConverter: ApiLogMessageConverter
) {
@ExceptionHandler(value = [RoomescapeException::class])
fun handleRoomException(e: RoomescapeException): ResponseEntity<CommonErrorResponse> {
val errorCode: ErrorCode = e.errorCode
val httpStatus: HttpStatus = errorCode.httpStatus
val errorResponse = CommonErrorResponse(errorCode)
val type = if (e is AuthException) LogType.AUTHENTICATION_FAILURE else LogType.APPLICATION_FAILURE
logException(
type = type,
httpStatus = httpStatus.value(),
errorResponse = errorResponse,
exception = e
)
return ResponseEntity
.status(errorCode.httpStatus)
.body(CommonErrorResponse(errorCode, e.message))
.status(httpStatus)
.body(errorResponse)
}
@ExceptionHandler(value = [HttpMessageNotReadableException::class])
fun handleHttpMessageNotReadableException(e: HttpMessageNotReadableException): ResponseEntity<CommonErrorResponse> {
log.debug { "message: ${e.message}" }
@ExceptionHandler(value = [MethodArgumentNotValidException::class, HttpMessageNotReadableException::class])
fun handleInvalidRequestValueException(e: Exception): ResponseEntity<CommonErrorResponse> {
val message: String = if (e is MethodArgumentNotValidException) {
e.bindingResult.allErrors
.mapNotNull { it.defaultMessage }
.joinToString(", ")
} else {
e.message!!
}
log.debug { "[ExceptionControllerAdvice] Invalid Request Value Exception occurred: $message" }
val errorCode: ErrorCode = CommonErrorCode.INVALID_INPUT_VALUE
return ResponseEntity
.status(errorCode.httpStatus)
.body(CommonErrorResponse(errorCode))
}
val httpStatus: HttpStatus = errorCode.httpStatus
val errorResponse = CommonErrorResponse(errorCode)
@ExceptionHandler(value = [MethodArgumentNotValidException::class])
fun handleMethodArgumentNotValidException(e: MethodArgumentNotValidException): ResponseEntity<CommonErrorResponse> {
val message: String = e.bindingResult.allErrors
.mapNotNull { it.defaultMessage }
.joinToString(", ")
log.debug { "message: $message" }
logException(
type = LogType.APPLICATION_FAILURE,
httpStatus = httpStatus.value(),
errorResponse = errorResponse,
exception = e
)
val errorCode: ErrorCode = CommonErrorCode.INVALID_INPUT_VALUE
return ResponseEntity
.status(errorCode.httpStatus)
.body(CommonErrorResponse(errorCode))
.status(httpStatus)
.body(errorResponse)
}
@ExceptionHandler(value = [Exception::class])
fun handleException(e: Exception): ResponseEntity<CommonErrorResponse> {
log.error(e) { "message: ${e.message}" }
log.error(e) { "[ExceptionControllerAdvice] Unexpected exception occurred: ${e.message}" }
val errorCode: ErrorCode = CommonErrorCode.UNEXPECTED_SERVER_ERROR
val httpStatus: HttpStatus = errorCode.httpStatus
val errorResponse = CommonErrorResponse(errorCode)
logException(
type = LogType.UNHANDLED_EXCEPTION,
httpStatus = httpStatus.value(),
errorResponse = errorResponse,
exception = e
)
return ResponseEntity
.status(errorCode.httpStatus)
.body(CommonErrorResponse(errorCode))
.status(httpStatus)
.body(errorResponse)
}
private fun logException(
type: LogType,
httpStatus: Int,
errorResponse: CommonErrorResponse,
exception: Exception
) {
val commonRequest = ConvertResponseMessageRequest(
type = type,
httpStatus = httpStatus,
startTime = MDC.get("startTime")?.toLongOrNull(),
body = errorResponse,
)
val logMessage = if (errorResponse.message == exception.message) {
messageConverter.convertToResponseMessage(commonRequest)
} else {
messageConverter.convertToResponseMessage(commonRequest.copy(exception = exception))
}
log.warn { logMessage }
}
}