Skip to content

Commit

Permalink
Merge pull request #53 from st-yclee/main
Browse files Browse the repository at this point in the history
MethodArgumentNotValidException Advice 처리
  • Loading branch information
st-yclee authored Sep 25, 2024
2 parents f92b99c + 0550b79 commit 3dbbb01
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/kr/co/mcmp/exception/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,6 +27,19 @@ protected ResponseWrapper<String> handleException(Exception e) {

}

/**
* MethodArgumentNotValidException 발생시
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ResponseWrapper<String>> 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 발생시
// */
Expand Down

0 comments on commit 3dbbb01

Please sign in to comment.