File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed
layer-api/src/main/java/org/layer/global/exception Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change 2020import org .springframework .web .method .annotation .MethodArgumentTypeMismatchException ;
2121import org .springframework .web .servlet .resource .NoResourceFoundException ;
2222
23+ import jakarta .validation .ConstraintViolationException ;
2324import lombok .RequiredArgsConstructor ;
2425import lombok .extern .slf4j .Slf4j ;
2526
@@ -102,4 +103,18 @@ public ResponseEntity<ExceptionResponse> handleConversionFailedException(Convers
102103 .body (ExceptionResponse .of (HttpStatus .BAD_REQUEST .name (), "요청 파라미터의 형식이 잘못되었습니다." ));
103104 }
104105
106+ @ ExceptionHandler (ConstraintViolationException .class )
107+ public ResponseEntity <ExceptionResponse > handleConstraintViolationException (ConstraintViolationException e ) {
108+ log .warn (String .format (LOG_FORMAT , e .getMessage ()), e );
109+
110+ String message = e .getConstraintViolations ()
111+ .stream ()
112+ .map (v -> v .getPropertyPath () + " " + v .getMessage ()) // 예: "title must not be null"
113+ .reduce ((m1 , m2 ) -> m1 + ", " + m2 )
114+ .orElse ("유효하지 않은 값입니다." );
115+
116+ return ResponseEntity .status (HttpStatus .BAD_REQUEST )
117+ .body (ExceptionResponse .of (HttpStatus .BAD_REQUEST .name (), message ));
118+ }
119+
105120}
You can’t perform that action at this time.
0 commit comments