Skip to content

Commit

Permalink
Feat: @Valid Exception 탐지 구체화
Browse files Browse the repository at this point in the history
  • Loading branch information
KoungQ committed Jul 15, 2024
1 parent 1972100 commit b623045
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.ErrorResponse;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

Expand All @@ -27,6 +28,13 @@ public CommonResponse<Void> handle(Exception ex) {
return CommonResponse.createFailure(status, ex.getMessage());
}

@ExceptionHandler(MethodArgumentNotValidException.class) // BindException == @ModelAttribute 어노테이션으로 받은 파라미터의 @Valid 통해 발생한 Exception
public CommonResponse<Void> handle(MethodArgumentNotValidException e) { // 클라이언트의 오류일 경우
int status = 400; // 파라미터 값 실수이므로 4XX
log.error("Error", e);
return CommonResponse.createFailure(status, e.getAllErrors().get(0).getDefaultMessage()); // 디폴트 메세지 가져오기
}

@ExceptionHandler(BindException.class) // BindException == @ModelAttribute 어노테이션으로 받은 파라미터의 @Valid 통해 발생한 Exception
public CommonResponse<Void> handle(BindException e) { // 클라이언트의 오류일 경우
int status = 400; // 파라미터 값 실수이므로 4XX
Expand Down

0 comments on commit b623045

Please sign in to comment.