|
18 | 18 | import org.springframework.web.server.ServerWebInputException; |
19 | 19 | import org.springframework.web.servlet.resource.NoResourceFoundException; |
20 | 20 |
|
| 21 | +import jakarta.validation.ConstraintViolationException; |
21 | 22 | import java.util.Arrays; |
22 | 23 | import java.util.regex.Matcher; |
23 | 24 | import java.util.regex.Pattern; |
@@ -119,6 +120,26 @@ public ResponseEntity<ApiResponse<?>> handleBadRequest(ServerWebInputException e |
119 | 120 | } |
120 | 121 | } |
121 | 122 |
|
| 123 | + @ExceptionHandler |
| 124 | + public ResponseEntity<ApiResponse<?>> handleBadRequest(IllegalArgumentException e) { |
| 125 | + log.warn("IllegalArgumentException : {}", e.getMessage()); |
| 126 | + return failureResponse(ErrorType.BAD_REQUEST, e.getMessage()); |
| 127 | + } |
| 128 | + |
| 129 | + @ExceptionHandler |
| 130 | + public ResponseEntity<ApiResponse<?>> handleBadRequest(ConstraintViolationException e) { |
| 131 | + String message = e.getConstraintViolations().stream() |
| 132 | + .map(v -> { |
| 133 | + String path = v.getPropertyPath().toString(); |
| 134 | + // "getRankings.page" -> "page" |
| 135 | + String field = path.contains(".") ? path.substring(path.lastIndexOf('.') + 1) : path; |
| 136 | + return String.format("'%s': %s", field, v.getMessage()); |
| 137 | + }) |
| 138 | + .collect(Collectors.joining(", ")); |
| 139 | + log.warn("ConstraintViolationException : {}", message); |
| 140 | + return failureResponse(ErrorType.BAD_REQUEST, message); |
| 141 | + } |
| 142 | + |
122 | 143 | @ExceptionHandler |
123 | 144 | public ResponseEntity<ApiResponse<?>> handleNotFound(NoResourceFoundException e) { |
124 | 145 | return failureResponse(ErrorType.NOT_FOUND, null); |
|
0 commit comments