File tree Expand file tree Collapse file tree
src/main/java/com/back/web7_9_codecrete_be/global/error/handler Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com .back .web7_9_codecrete_be .global .error .handler ;
22
3- import org .springframework .http .ResponseEntity ;
4- import org .springframework .web .bind .annotation .ExceptionHandler ;
5- import org .springframework .web .bind .annotation .RestControllerAdvice ;
6-
73import com .back .web7_9_codecrete_be .global .error .exception .BusinessException ;
84import com .back .web7_9_codecrete_be .global .error .util .ErrorResponse ;
95import com .back .web7_9_codecrete_be .global .rsData .RsData ;
10-
116import lombok .extern .slf4j .Slf4j ;
7+ import org .springframework .http .ResponseEntity ;
8+ import org .springframework .web .bind .MethodArgumentNotValidException ;
9+ import org .springframework .web .bind .annotation .ExceptionHandler ;
10+ import org .springframework .web .bind .annotation .RestControllerAdvice ;
1211
1312@ Slf4j
1413@ RestControllerAdvice
@@ -20,4 +19,24 @@ public ResponseEntity<RsData<Void>> handleBusinessException(BusinessException ex
2019 return ErrorResponse .build (ex .getErrorCode ());
2120 }
2221
22+ // @Valid 검증 실패 처리
23+ @ ExceptionHandler (MethodArgumentNotValidException .class )
24+ public ResponseEntity <RsData <Void >> handleValidationException (
25+ MethodArgumentNotValidException ex
26+ ) {
27+ String message = ex .getBindingResult ()
28+ .getFieldErrors ()
29+ .stream ()
30+ .findFirst ()
31+ .map (error -> error .getDefaultMessage ())
32+ .orElse ("입력값이 올바르지 않습니다." );
33+
34+ return ResponseEntity
35+ .badRequest ()
36+ .body (new RsData <>(
37+ 400 ,
38+ "VALIDATION_ERROR" ,
39+ message
40+ ));
41+ }
2342}
You can’t perform that action at this time.
0 commit comments