전체 구조
Custom Exception 처리 방식
- 비슷한 역할이나 동일한 예외에 대해서 공통 예외로 추상화 합니다.
- NotFound
- ProgramNotFound
- BoothNotFound
- Forbidden
- ForbiddenUpdate
- ForbiddenDelete
- NotFound
- 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));
}
'Project > WithFestival' 카테고리의 다른 글
조회수 중복 증가 방지(Redis + IP검증) (0) | 2023.09.26 |
---|