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