Skip to content

Commit 53dcca7

Browse files
committed
fix: 클라이언트 에러 핸들링
1 parent 60dabe0 commit 53dcca7

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

layer-api/src/main/java/org/layer/global/exception/GlobalExceptionHandler.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
2121
import org.springframework.web.servlet.resource.NoResourceFoundException;
2222

23+
import jakarta.validation.ConstraintViolationException;
2324
import lombok.RequiredArgsConstructor;
2425
import lombok.extern.slf4j.Slf4j;
2526

@@ -102,4 +103,18 @@ public ResponseEntity<ExceptionResponse> handleConversionFailedException(Convers
102103
.body(ExceptionResponse.of(HttpStatus.BAD_REQUEST.name(), "요청 파라미터의 형식이 잘못되었습니다."));
103104
}
104105

106+
@ExceptionHandler(ConstraintViolationException.class)
107+
public ResponseEntity<ExceptionResponse> handleConstraintViolationException(ConstraintViolationException e) {
108+
log.warn(String.format(LOG_FORMAT, e.getMessage()), e);
109+
110+
String message = e.getConstraintViolations()
111+
.stream()
112+
.map(v -> v.getPropertyPath() + " " + v.getMessage()) // 예: "title must not be null"
113+
.reduce((m1, m2) -> m1 + ", " + m2)
114+
.orElse("유효하지 않은 값입니다.");
115+
116+
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
117+
.body(ExceptionResponse.of(HttpStatus.BAD_REQUEST.name(), message));
118+
}
119+
105120
}

0 commit comments

Comments
 (0)