Skip to content

Commit

Permalink
[#4] feat: handlerMethodValidation에 대한 예외처리 추가 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
NaMinhyeok committed Jan 21, 2025
1 parent 310a214 commit 9af867b
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.HandlerMethodValidationException;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@Slf4j
@RestControllerAdvice
Expand Down Expand Up @@ -58,6 +60,20 @@ public ResponseEntity<ApiResponse<?>> handleBindException(BindException e) {
return new ResponseEntity<>(ApiResponse.error(ErrorType.BINDING_ERROR, validationErrors), ErrorType.BINDING_ERROR.getStatus());
}

@ExceptionHandler(HandlerMethodValidationException.class)
public ResponseEntity<ApiResponse<?>> handleHandlerMethodValidationException(HandlerMethodValidationException e) {
log.warn("Validation error occurred: {}", e.getMessage(), e);

List<Map<String, String>> validationErrors = e.getValueResults().stream()
.map(result -> Map.of(
"field", result.getMethodParameter().getParameterName(),
"message", result.getResolvableErrors().getFirst().getDefaultMessage()
))
.toList();

return new ResponseEntity<>(ApiResponse.error(ErrorType.METHOD_ARGUMENT_NOT_VALID, validationErrors), ErrorType.METHOD_ARGUMENT_NOT_VALID.getStatus());
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ApiResponse<?>> handleException(Exception e) {
log.error("Exception : {}", e.getMessage(), e);
Expand Down

0 comments on commit 9af867b

Please sign in to comment.