Skip to content

Commit 2328ec6

Browse files
authored
[BUG] MethodArgumentTypeMismatchException 핸들링 (#176)
* fix: MethodArgumentTypeMismatchException 핸들링 (#172) * refactor: 코드 리팩토링 (#172)
1 parent 7220178 commit 2328ec6

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/main/java/com/smunity/server/global/exception/code/ErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum ErrorCode {
1313
UNAUTHORIZED(401, "COMMON002", "로그인이 필요합니다."),
1414
METHOD_NOT_ALLOWED(405, "COMMON003", "지원하지 않는 Http Method 입니다."),
1515
FORBIDDEN(403, "COMMON004", "금지된 요청입니다."),
16+
INVALID_ENUM_VALUE(400, "COMMON005", "잘못된 요청 값입니다."),
1617

1718
// Validation Errors
1819
VALIDATION_FAILED(400, "VALID001", "입력값에 대한 검증에 실패했습니다."),

src/main/java/com/smunity/server/global/exception/handler/GeneralExceptionHandler.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.web.bind.annotation.ExceptionHandler;
1313
import org.springframework.web.bind.annotation.RestControllerAdvice;
1414
import org.springframework.web.method.annotation.HandlerMethodValidationException;
15+
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
1516

1617
import java.util.Map;
1718
import java.util.Objects;
@@ -24,48 +25,49 @@ public class GeneralExceptionHandler {
2425
@ExceptionHandler(GeneralException.class)
2526
protected ResponseEntity<ErrorResponse<Void>> handleGeneralException(GeneralException ex) {
2627
log.warn("[WARNING] {} : {}", ex.getClass(), ex.getMessage());
27-
ErrorCode errorCode = ex.getErrorCode();
28-
return ErrorResponse.handle(errorCode);
28+
return ErrorResponse.handle(ex.getErrorCode());
2929
}
3030

3131
// 요청 파라미터 검증 실패(MethodArgumentNotValidException) 처리 메서드
3232
@ExceptionHandler(MethodArgumentNotValidException.class)
3333
protected ResponseEntity<ErrorResponse<Map<String, String>>> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
3434
log.warn("[WARNING] {} : {}", ex.getClass(), ex.getMessage());
35-
ErrorCode errorCode = ErrorCode.VALIDATION_FAILED;
36-
return ErrorResponse.handle(errorCode, ex.getFieldErrors());
35+
return ErrorResponse.handle(ErrorCode.VALIDATION_FAILED, ex.getFieldErrors());
3736
}
3837

3938
// 데이터 무결성 위반(DataIntegrityViolationException) 처리 메서드
4039
@ExceptionHandler(DataIntegrityViolationException.class)
4140
protected ResponseEntity<ErrorResponse<Void>> handleDataIntegrityViolationException(DataIntegrityViolationException ex) {
4241
log.warn("[WARNING] {} : {}", ex.getClass(), ex.getMessage());
43-
ErrorCode errorCode = ErrorCode.VALIDATION_FAILED;
44-
return ErrorResponse.handle(errorCode);
42+
return ErrorResponse.handle(ErrorCode.VALIDATION_FAILED);
4543
}
4644

4745
// 컨트롤러 메서드 파라미터의 유효성 검증 실패(HandlerMethodValidationException) 처리 메서드 - @PermissionCheckValidator
4846
@ExceptionHandler(HandlerMethodValidationException.class)
4947
protected ResponseEntity<ErrorResponse<Void>> handleHandlerMethodValidationException(HandlerMethodValidationException ex) {
5048
log.warn("[WARNING] {} : {}", ex.getClass(), ex.getMessage());
51-
ErrorCode errorCode = extractErrorCode(ex);
52-
return ErrorResponse.handle(errorCode);
49+
return ErrorResponse.handle(extractErrorCode(ex));
5350
}
5451

5552
// 지원되지 않는 HTTP 메서드 요청(HttpRequestMethodNotSupportedException) 처리 메서드
5653
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
5754
protected ResponseEntity<ErrorResponse<Void>> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
5855
log.warn("[WARNING] {} : {}", ex.getClass(), ex.getMessage());
59-
ErrorCode errorCode = ErrorCode.METHOD_NOT_ALLOWED;
60-
return ErrorResponse.handle(errorCode);
56+
return ErrorResponse.handle(ErrorCode.METHOD_NOT_ALLOWED);
57+
}
58+
59+
// 메서드 인자 타입 불일치(MethodArgumentTypeMismatchException) 처리 메서드
60+
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
61+
protected ResponseEntity<ErrorResponse<Void>> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException ex) {
62+
log.warn("[WARNING] {} : {}", ex.getClass(), ex.getMessage());
63+
return ErrorResponse.handle(ErrorCode.INVALID_ENUM_VALUE);
6164
}
6265

6366
// 기타 모든 예외(Exception) 처리 메서드
6467
@ExceptionHandler(Exception.class)
6568
protected ResponseEntity<ErrorResponse<Void>> handleException(Exception ex) {
6669
log.error("[ERROR] {} : {}", ex.getClass(), ex.getMessage(), ex);
67-
ErrorCode errorCode = ErrorCode.INTERNAL_SERVER_ERROR;
68-
return ErrorResponse.handle(errorCode);
70+
return ErrorResponse.handle(ErrorCode.INTERNAL_SERVER_ERROR);
6971
}
7072

7173
// HandlerMethodValidationException 에서 ErrorCode 를 추출하는 메서드

0 commit comments

Comments
 (0)