diff --git a/src/main/java/kr/co/mcmp/exception/GlobalExceptionHandler.java b/src/main/java/kr/co/mcmp/exception/GlobalExceptionHandler.java index a6eddb7..157437d 100644 --- a/src/main/java/kr/co/mcmp/exception/GlobalExceptionHandler.java +++ b/src/main/java/kr/co/mcmp/exception/GlobalExceptionHandler.java @@ -1,10 +1,15 @@ package kr.co.mcmp.exception; +import kr.co.mcmp.response.ResponseCode; import kr.co.mcmp.response.ResponseWrapper; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import java.util.stream.Collectors; + @Slf4j @RestControllerAdvice @@ -22,6 +27,19 @@ protected ResponseWrapper handleException(Exception e) { } + /** + * MethodArgumentNotValidException 발생시 + */ + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity> handleValidationException(MethodArgumentNotValidException e) { + String errors = e.getBindingResult().getFieldErrors().stream() + .filter(fieldError -> fieldError.getRejectedValue() == null || + fieldError.getRejectedValue().toString().trim().isEmpty()) + .map(fieldError -> String.format("'%s' %s", fieldError.getField(), fieldError.getDefaultMessage())) + .collect(Collectors.joining("\n")); + return ResponseEntity.ok().body(new ResponseWrapper<>(ResponseCode.BAD_REQUEST, errors)); + } + // /** // * RestClientResponseException 발생시 // */