RestExceptionHandler

REST 예외 핸들러. Spring @ControllerAdvice로 전역 예외 처리.

클래스 정의

@ControllerAdvice
class RestExceptionHandler(
    private val messageSourceService: MessageSourceService,
) : ResponseEntityExceptionHandler()

예외 핸들러 목록

AuthorizationFailedException

@ExceptionHandler(AuthorizationFailedException::class)
fun handle(e: AuthorizationFailedException, request: HttpServletRequest): ResponseEntity<Any>?

HTTP 401 UNAUTHORIZED 반환.

EntityNotFoundException

@ExceptionHandler(EntityNotFoundException::class)
fun handle(e: EntityNotFoundException, request: HttpServletRequest): ResponseEntity<Any>?

HTTP 404 NOT_FOUND 반환.

MethodArgumentInvalidException

@ExceptionHandler(MethodArgumentInvalidException::class)
fun handle(e: MethodArgumentInvalidException, request: HttpServletRequest): ResponseEntity<Any>?

HTTP 400 BAD_REQUEST 반환.

ConstraintViolationException

@ExceptionHandler(ConstraintViolationException::class)
fun handle(e: ConstraintViolationException, request: HttpServletRequest): ResponseEntity<Any>?

HTTP 400 BAD_REQUEST 반환. Bean Validation 오류용.

CacheKeyInvalidException

@ExceptionHandler(CacheKeyInvalidException::class)
fun handle(e: CacheKeyInvalidException, request: HttpServletRequest): ResponseEntity<Any>?

HTTP 410 GONE 반환.

SystemMaintenanceException

@ExceptionHandler(SystemMaintenanceException::class)
fun handle(e: SystemMaintenanceException, request: HttpServletRequest): ResponseEntity<Any>

HTTP 550 (커스텀) 반환.

InternationalSearchException

@ExceptionHandler(InternationalSearchException::class)
fun handle(e: InternationalSearchException, request: HttpServletRequest): ResponseEntity<Any>?

HTTP 500 INTERNAL_SERVER_ERROR 반환.

ResponseStatusException

Spring ResponseStatusException 처리.

Exception

모든 예외의 기본 핸들러. HTTP 500 반환.

유틸리티 함수

log()

fun <T : Throwable> T.log(): T

에러 로깅 (스택 트레이스 포함).

logOnlyMessage()

private fun <T : Throwable> T.logOnlyMessage(): T

메시지만 로깅 (스택 트레이스 제외).

sentryLog()

fun <T : Throwable> T.sentryLog(): T

Sentry에 예외 전송. MDC 컨텍스트 포함.

findRootCause()

fun Throwable.findRootCause(): Throwable

근본 원인 예외 찾기.

Sentry 연동

  • MDC 컨텍스트를 Sentry scope에 설정
  • Datadog trace/span ID는 태그로 설정
  • notifiable 플래그로 알림 제어
  • capturable=false면 Sentry 전송 안 함

특징

  • @ControllerAdvice: 전역 예외 처리
  • ErrorView 응답: 통일된 에러 응답 형식
  • Sentry 연동: 에러 모니터링
  • MDC 컨텍스트: 로깅 컨텍스트 유지