refactor: ExceptionHandler에서의 로깅 방식 수정

- 커스텀 예외(RoomescapeException)은 로깅 X -> 에러 발생 지점에서 기록
- Exception인 경우에만 stacktrace 기록
This commit is contained in:
이상진 2025-07-27 22:39:41 +09:00
parent d34a6ad27e
commit 90c1cff233

View File

@ -11,48 +11,46 @@ import roomescape.common.dto.response.CommonErrorResponse
@RestControllerAdvice
class ExceptionControllerAdvice(
private val logger: KLogger = KotlinLogging.logger {}
private val log: KLogger = KotlinLogging.logger {}
) {
@ExceptionHandler(value = [RoomescapeException::class])
fun handleRoomException(e: RoomescapeException): ResponseEntity<CommonErrorResponse> {
logger.error(e) { "message: ${e.message}" }
val errorCode: ErrorCode = e.errorCode
return ResponseEntity
.status(errorCode.httpStatus)
.body(CommonErrorResponse(errorCode, e.message))
.status(errorCode.httpStatus)
.body(CommonErrorResponse(errorCode, e.message))
}
@ExceptionHandler(value = [HttpMessageNotReadableException::class])
fun handleHttpMessageNotReadableException(e: HttpMessageNotReadableException): ResponseEntity<CommonErrorResponse> {
logger.error(e) { "message: ${e.message}" }
log.debug { "message: ${e.message}" }
val errorCode: ErrorCode = CommonErrorCode.INVALID_INPUT_VALUE
return ResponseEntity
.status(errorCode.httpStatus)
.body(CommonErrorResponse(errorCode))
.status(errorCode.httpStatus)
.body(CommonErrorResponse(errorCode))
}
@ExceptionHandler(value = [MethodArgumentNotValidException::class])
fun handleMethodArgumentNotValidException(e: MethodArgumentNotValidException): ResponseEntity<CommonErrorResponse> {
val message: String = e.bindingResult.allErrors
.mapNotNull { it.defaultMessage }
.joinToString(", ")
logger.error(e) { "message: $message" }
.mapNotNull { it.defaultMessage }
.joinToString(", ")
log.debug { "message: $message" }
val errorCode: ErrorCode = CommonErrorCode.INVALID_INPUT_VALUE
return ResponseEntity
.status(errorCode.httpStatus)
.body(CommonErrorResponse(errorCode))
.status(errorCode.httpStatus)
.body(CommonErrorResponse(errorCode))
}
@ExceptionHandler(value = [Exception::class])
fun handleException(e: Exception): ResponseEntity<CommonErrorResponse> {
logger.error(e) { "message: ${e.message}" }
log.error(e) { "message: ${e.message}" }
val errorCode: ErrorCode = CommonErrorCode.UNEXPECTED_SERVER_ERROR
return ResponseEntity
.status(errorCode.httpStatus)
.body(CommonErrorResponse(errorCode))
.status(errorCode.httpStatus)
.body(CommonErrorResponse(errorCode))
}
}