API 레이어는 서비스 레이어에 대해 의존성을 가지고 있고, 서비스 레이어의 메서드를 호출하고 있으므로 서비스 레이어에서 발생한 예외는 API로 던져지게 되고, 다시 API 레이어에서의 발생한 예외들은 앞서 작성한 @RestControllerAdvice@ExceptionHandler 를 통해 관리할 수 있다.

서비스 레이어에서 발생할 수 있는 경우에 맞게 커스텀한 예외클래스와 Enum 클래스를 만들어 여러 예외 상황을 처리할 수 있다.

런타임 환경에서 발생할 수 있는 unchecked Exception 을 처리해야 하므로 먼저 어드바이스에 RuntimeException을 캐치할 수 있도록 코드를 추가한다.

@ExceptionHandler(value = RuntimeException.class)
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public ErrorResponse handlerResourceNotFoundException(BusinessRuntimeException e) {
    System.out.println(e.getMessage());
    return null;
}

CustomException

@Getter
@RequiredArgsConstructor
public enum CustomException {

    MEMBER_NOT_FOUND(404, "No Matched Member Found");

    private final int code;
    private final String desc;

}
public class BusinessRuntimeException extends RuntimeException{

    @Getter
    private CustomException customException;

    public BusinessRuntimeException(String message, CustomException customException) {
        super(customException.getMessage());
        this.customException = customException;
    }
}