diff --git a/src/main/java/com/onebridge/ouch/apiPayload/code/error/CommonErrorCode.java b/src/main/java/com/onebridge/ouch/apiPayload/code/error/CommonErrorCode.java index a038c74..70bfe8e 100644 --- a/src/main/java/com/onebridge/ouch/apiPayload/code/error/CommonErrorCode.java +++ b/src/main/java/com/onebridge/ouch/apiPayload/code/error/CommonErrorCode.java @@ -11,6 +11,7 @@ public enum CommonErrorCode implements ErrorCode { // 가장 일반적인 응답 INVALID_PARAMETER(HttpStatus.BAD_REQUEST, "COMMON400", "파라미터가 올바르지 않습니다."), + INVALID_BODY(HttpStatus.BAD_REQUEST, "COMMON400", "요청 본문이 올바르지 않습니다."), _INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON500", "서버 에러, 관리자에게 문의 바랍니다."), _BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON400", "잘못된 요청입니다."), _UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "COMMON401", "인증이 필요합니다."), @@ -18,7 +19,6 @@ public enum CommonErrorCode implements ErrorCode { RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMON404", "찾을 수 없는 리소스입니다."), METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "COMMON405", "허용되지 않는 HTTP Method입니다."), - // 멤버 관려 에러 MEMBER_NOT_FOUND(HttpStatus.BAD_REQUEST, "MEMBER4001", "사용자가 없습니다."), NICKNAME_NOT_EXIST(HttpStatus.BAD_REQUEST, "MEMBER4002", "닉네임은 필수 입니다."), diff --git a/src/main/java/com/onebridge/ouch/apiPayload/handler/ExceptionAdvice.java b/src/main/java/com/onebridge/ouch/apiPayload/handler/ExceptionAdvice.java index 71e07d0..5c37a1e 100644 --- a/src/main/java/com/onebridge/ouch/apiPayload/handler/ExceptionAdvice.java +++ b/src/main/java/com/onebridge/ouch/apiPayload/handler/ExceptionAdvice.java @@ -3,6 +3,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.validation.ObjectError; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; @@ -12,6 +13,7 @@ import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; +import com.fasterxml.jackson.databind.exc.InvalidFormatException; import com.onebridge.ouch.apiPayload.ApiResponse; import com.onebridge.ouch.apiPayload.code.error.CommonErrorCode; import com.onebridge.ouch.apiPayload.code.error.ErrorCode; @@ -53,6 +55,28 @@ public ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotVali return handleExceptionInternal(errorCode, getDefaultMessage(e)); } + @Override + public ResponseEntity handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, + HttpStatusCode status, WebRequest request) { + + // 상세 메시지 구성 : 내부 cause가 InvalidFormatException 인 경우(@RequestBody + Jackson이 DTO로 바꾸려다가 실패할 때) + Throwable cause = ex.getCause(); + if (cause instanceof InvalidFormatException) { + InvalidFormatException ife = (InvalidFormatException)cause; + String fieldName = ife.getPath().get(0).getFieldName(); + String value = ife.getValue().toString(); + String targetType = ife.getTargetType().getSimpleName(); + + String message = String.format("'%s'는 %s 필드에 유효하지 않은 값입니다. (%s 타입)", value, fieldName, targetType); + + ErrorCode errorCode = CommonErrorCode.INVALID_BODY; + return handleExceptionInternal(errorCode, message); + } + + ErrorCode errorCode = CommonErrorCode.INVALID_BODY; + return handleExceptionInternal(errorCode, ex.getMessage()); + } + private static String getDefaultMessage(MethodArgumentNotValidException e) { StringBuilder message = new StringBuilder(); for (ObjectError error : e.getBindingResult().getAllErrors()) {