@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);
}
}
'프로젝트 > Ku:room' 카테고리의 다른 글
개발자 겸 디자이너 겸 PM 도전기 (0) | 2025.02.14 |
---|---|
nGrinder 스크립트 검증 도중 Connection refused (1) | 2025.02.13 |
QueryDSL로 동적 쿼리 가져오기 (0) | 2025.02.13 |
SecurityConfig 내 이용한 허용 URI Enum 클래스로 관리하기 (0) | 2025.02.13 |
이메일 인증 기능 @Async 비동기 처리 (0) | 2025.02.08 |