File tree Expand file tree Collapse file tree
src/main/java/com/xpeho/spring_boot_java_random_user/presentation/exceptions Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33import com .xpeho .spring_boot_java_random_user .domain .exceptions .InvalidPaginationException ;
44import com .xpeho .spring_boot_java_random_user .domain .exceptions .UserNotFoundException ;
5+ import jakarta .validation .ConstraintViolationException ;
56import org .slf4j .Logger ;
67import org .slf4j .LoggerFactory ;
78import org .springframework .http .HttpStatus ;
1415public class GlobalExceptionHandler {
1516 private static final Logger logger = LoggerFactory .getLogger (GlobalExceptionHandler .class );
1617
18+ @ ExceptionHandler (ConstraintViolationException .class )
19+ public ResponseEntity <ErrorResponse > handleConstraintViolationException (ConstraintViolationException ex ) {
20+ String message = ex .getConstraintViolations ().stream ()
21+ .map (v -> v .getPropertyPath () + ": " + v .getMessage ())
22+ .findFirst ()
23+ .orElse (ex .getMessage ());
24+ logger .warn ("Constraint violation: {}" , message );
25+ return buildErrorResponse ("INVALID_PARAMETER" , message , HttpStatus .BAD_REQUEST );
26+ }
27+
1728 @ ExceptionHandler (InvalidPaginationException .class )
1829 public ResponseEntity <ErrorResponse > handleInvalidPaginationException (InvalidPaginationException ex ) {
1930 logger .warn ("Invalid pagination request: {}" , ex .getMessage ());
You can’t perform that action at this time.
0 commit comments