Skip to content

Commit 04d77a5

Browse files
authored
chore: MethodArgumentTypeMismatchException 핸들링 추가 (#448)
1 parent 4efe4b9 commit 04d77a5

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.layer.common.exception.ExceptionType;
99
import org.layer.discord.notifier.ErrorEvent;
1010
import org.springframework.context.ApplicationEventPublisher;
11+
import org.springframework.core.convert.ConversionFailedException;
1112
import org.springframework.http.HttpStatus;
1213
import org.springframework.http.ResponseEntity;
1314
import org.springframework.http.converter.HttpMessageNotReadableException;
@@ -16,6 +17,7 @@
1617
import org.springframework.web.bind.MissingServletRequestParameterException;
1718
import org.springframework.web.bind.annotation.ExceptionHandler;
1819
import org.springframework.web.bind.annotation.RestControllerAdvice;
20+
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
1921
import org.springframework.web.servlet.resource.NoResourceFoundException;
2022

2123
import lombok.RequiredArgsConstructor;
@@ -81,4 +83,23 @@ public ResponseEntity<ExceptionResponse> handleMissingServletRequestParameterExc
8183
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
8284
.body(ExceptionResponse.of(HttpStatus.BAD_REQUEST.name(), "유효하지 않은 값."));
8385
}
86+
87+
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
88+
public ResponseEntity<ExceptionResponse> handleMethodArgumentTypeMismatchException(
89+
MethodArgumentTypeMismatchException e) {
90+
log.warn(String.format(LOG_FORMAT, e.getMessage()), e);
91+
String paramName = e.getName();
92+
String invalidValue = e.getValue() != null ? e.getValue().toString() : "null";
93+
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
94+
.body(ExceptionResponse.of(HttpStatus.BAD_REQUEST.name(),
95+
String.format("파라미터 '%s'의 값 '%s'은(는) 유효하지 않습니다.", paramName, invalidValue)));
96+
}
97+
98+
@ExceptionHandler(ConversionFailedException.class)
99+
public ResponseEntity<ExceptionResponse> handleConversionFailedException(ConversionFailedException e) {
100+
log.warn(String.format(LOG_FORMAT, e.getMessage()), e);
101+
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
102+
.body(ExceptionResponse.of(HttpStatus.BAD_REQUEST.name(), "요청 파라미터의 형식이 잘못되었습니다."));
103+
}
104+
84105
}

0 commit comments

Comments
 (0)