Project/WithFestival

예외 공통 처리 프로젝트 적용 회고

Seung__Yong 2023. 9. 1. 01:41

전체 구조

Custom Exception 처리 방식 

  • 비슷한 역할이나 동일한 예외에 대해서 공통 예외로 추상화 합니다.
    • NotFound
      • ProgramNotFound
      • BoothNotFound
    • Forbidden
      • ForbiddenUpdate
      • ForbiddenDelete
  • ErrorCode(Enum Type)를 통해 구분하며 에러 코드는 에러 메시지와 HTTP 상태 값을 가집니다.
  • 처리 방법이 나뉘지 않는다면 모두 CustomException으로 묶어 처리하게 됩니다.
    @ExceptionHandler(CustomException.class)
    public ResponseEntity<ExceptionResponse> handleCustomException(final Custom e) {
        log.warn(e.ErrorCode.getMessage(), e);

        return ResponseEntity.badRequest()
                .body(new ExceptionResponse(e.ErrorCode.getCode(), e.ErrorCode.getMessage()));
    }

 

추가 예외 처리 방식

  • 추가적인 예외를 발견했을 때 위와 같은 형태로 처리가 가능하다면 CustomException으로 만들어 처리하고 다른 작업이 필요하다면 따로 핸들링 합니다.
    @ExceptionHandler(SizeLimitExceededException.class)
    public ResponseEntity<ExceptionResponse> handleSizeLimitExceededException(final SizeLimitExceededException e) {
        log.warn(e.getMessage(), e);

        final String message = EXCEED_IMAGE_CAPACITY.getMessage()
                + " 입력된 이미지 용량은 " + e.getActualSize() + " byte 입니다. "
                + "(제한 용량: " + e.getPermittedSize() + " byte)";
        return ResponseEntity.badRequest()
                .body(new ExceptionResponse(EXCEED_IMAGE_CAPACITY.getCode(), message));
    }