프로젝트/Ku:room

각 상황에 맞는 @ControllerAdvice로 예외 메시지 분리하기

개발하는 민우 2025. 2. 13. 00:53

@ControllerAdvice는 스프링 애플리케이션 전체에서 예외 처리 메서드를 선언할 수 있는 특수한 스프링 빈이다.

 

본 프로젝트에서는 @BaseControllerAdvice랑, 각각의 도메인별 @ControllerAdvice를 설정해주었다.

 

@BaseControllerAdvice

CustomException 외의 나머지 예외를 처리하기 위해 다양한 예외 상황에 대비할 수 있는 폴백 기능을 추가하였다.

@Slf4j
@RestControllerAdvice
public class BaseControllerAdvice {

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public BaseErrorResponse handleServerException(final Exception e) {
        log.error("[handle InternalServerException]", e);
        return new BaseErrorResponse(SERVER_ERROR);
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({BadRequestException.class, NoHandlerFoundException.class, TypeMismatchException.class})
    public BaseErrorResponse handleBadRequest(final Exception e) {
        log.error("[handle BadRequest]", e);
        return new BaseErrorResponse(URL_NOT_FOUND);
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public BaseErrorResponse handle_HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        log.error("[handle_HttpRequestMethodNotSupportedException]", e);
        return new BaseErrorResponse(METHOD_NOT_ALLOWED);
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ConstraintViolationException.class)
    public BaseErrorResponse handle_ConstraintViolationException(ConstraintViolationException e) {
        log.error("[handle_ConstraintViolationException]", e);
        return new BaseErrorResponse(BAD_REQUEST);
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(InternalServerErrorException.class)
    public BaseErrorResponse handle_InternalServerError(InternalServerErrorException e) {
        log.error("[handle_InternalServerError]", e);
        return new BaseErrorResponse(INTERNAL_SERVER_ERROR);
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(RuntimeException.class)
    public BaseErrorResponse handle_RuntimeException(Exception e) {
        log.error("[handle_RuntimeException]", e);
        return new BaseErrorResponse(INTERNAL_SERVER_ERROR);
    }

}

 

@UserExceptionControllerAdvice

유저에 관한 예외를 처리 하기 위한 폴백 기능을 추가하였다.

Priority(0)을 통해 BaseControllerAdvice보다 우선시 하여, 먼저 대응하기 위해 설정하였다.

@Slf4j
@Priority(0)
@RestControllerAdvice
public class UserExceptionControllerAdvice {
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoSuchUserException.class)
    public BaseErrorResponse handleNoSuchUserException(final NoSuchUserException e) {
        log.error("[handleNoSuchUserException]");
        return new BaseErrorResponse(NO_SUCH_USER);
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(MailSendException.class)
    public BaseErrorResponse handleMailSendException(final MailSendException e) {
        log.error("[handleMailSendException]");
        return new BaseErrorResponse(MAIL_SEND_EXCEPTION);
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(DuplicateStudentIdException.class)
    public BaseErrorResponse handleDuplicateStudentIdException(final DuplicateStudentIdException e) {
        log.error("[handleDuplicateStudentIdException]");
        return new BaseErrorResponse(DUPLICATE_STUDENT_ID);
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(DuplicateEmailException.class)
    public BaseErrorResponse handleDuplicateEmailException(final DuplicateEmailException e) {
        log.error("[handleDuplicateEmailException]");
        return new BaseErrorResponse(DUPLICATE_EMAIL);
    }

}