Skip to content

Commit d5a7184

Browse files
committed
Fix: API 요청에 LocalDate 타입 오류 시 예외 처리
1 parent 588ab22 commit d5a7184

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/main/java/com/ktb/assignment/exception/ErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public enum ErrorCode {
1212
MISSING_REQUEST_PARAMETER("40001", HttpStatus.BAD_REQUEST, "필수 파라미터가 누락되었습니다."),
1313
MISSING_REQUEST_BODY("40002", HttpStatus.BAD_REQUEST, "요청 바디가 누락되었습니다."),
1414
MISSING_API_KEY_PARAMETER("40003", HttpStatus.BAD_REQUEST, "API Key가 누락되었습니다."),
15+
INVALID_DATE_FORMAT("40004", HttpStatus.BAD_REQUEST, "날짜 형식이 올바르지 않습니다. (yyyy-MM-dd) 형식을 사용하세요."),
1516

1617
// 403 error
1718
ACCESS_DENIED_ERROR("40300", HttpStatus.FORBIDDEN, "API Key가 일치하지 않습니다."),

src/main/java/com/ktb/assignment/exception/GlobalExceptionHandler.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
import org.springframework.web.HttpRequestMethodNotSupportedException;
88
import org.springframework.web.bind.annotation.ExceptionHandler;
99
import org.springframework.web.bind.annotation.RestControllerAdvice;
10+
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
1011
import org.springframework.web.multipart.support.MissingServletRequestPartException;
1112
import org.springframework.web.servlet.NoHandlerFoundException;
1213

14+
import java.time.LocalDate;
15+
1316
@Slf4j
1417
@RestControllerAdvice
1518
public class GlobalExceptionHandler {
@@ -47,6 +50,22 @@ public ResponseEntity<ResponseDto<?>> handleMessageNotReadableException(HttpMess
4750
.body(ResponseDto.fail(exception));
4851
}
4952

53+
// API 요청 LocalDate 인자 타입이 틀린 경우 (400)
54+
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
55+
public ResponseEntity<ResponseDto<?>> handleArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
56+
log.error("handleArgumentTypeMismatchException: {}", e.getMessage());
57+
58+
if (e.getRequiredType() != null && e.getRequiredType().equals(LocalDate.class)) {
59+
return ResponseEntity
60+
.status(ErrorCode.INVALID_DATE_FORMAT.getHttpStatus())
61+
.body(ResponseDto.fail(new CommonException(ErrorCode.INVALID_DATE_FORMAT)));
62+
}
63+
64+
return ResponseEntity
65+
.status(ErrorCode.INVALID_PARAMETER.getHttpStatus())
66+
.body(ResponseDto.fail(new CommonException(ErrorCode.INVALID_PARAMETER)));
67+
}
68+
5069
// 개발자가 직접 정의한 커스텀 예외
5170
@ExceptionHandler(CommonException.class)
5271
public ResponseEntity<ResponseDto<?>> handleApiException(CommonException e) {

0 commit comments

Comments
 (0)