Skip to content

Commit 895e267

Browse files
committed
refactor: Validation 예외 전역 처리 추가
1 parent ec6443d commit 895e267

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.back.web7_9_codecrete_be.global.error.handler;
22

3-
import org.springframework.http.ResponseEntity;
4-
import org.springframework.web.bind.annotation.ExceptionHandler;
5-
import org.springframework.web.bind.annotation.RestControllerAdvice;
6-
73
import com.back.web7_9_codecrete_be.global.error.exception.BusinessException;
84
import com.back.web7_9_codecrete_be.global.error.util.ErrorResponse;
95
import com.back.web7_9_codecrete_be.global.rsData.RsData;
10-
116
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.MethodArgumentNotValidException;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.bind.annotation.RestControllerAdvice;
1211

1312
@Slf4j
1413
@RestControllerAdvice
@@ -20,4 +19,24 @@ public ResponseEntity<RsData<Void>> handleBusinessException(BusinessException ex
2019
return ErrorResponse.build(ex.getErrorCode());
2120
}
2221

22+
// @Valid 검증 실패 처리
23+
@ExceptionHandler(MethodArgumentNotValidException.class)
24+
public ResponseEntity<RsData<Void>> handleValidationException(
25+
MethodArgumentNotValidException ex
26+
) {
27+
String message = ex.getBindingResult()
28+
.getFieldErrors()
29+
.stream()
30+
.findFirst()
31+
.map(error -> error.getDefaultMessage())
32+
.orElse("입력값이 올바르지 않습니다.");
33+
34+
return ResponseEntity
35+
.badRequest()
36+
.body(new RsData<>(
37+
400,
38+
"VALIDATION_ERROR",
39+
message
40+
));
41+
}
2342
}

0 commit comments

Comments
 (0)