From dbf262d53d9fc9b25d3c58959b1213298f5bf8ca Mon Sep 17 00:00:00 2001 From: kdy2224 Date: Thu, 1 May 2025 03:23:45 +0900 Subject: [PATCH] =?UTF-8?q?[REFACT]=20=EC=9E=90=EA=B0=80=EC=A7=84=EB=8B=A8?= =?UTF-8?q?=ED=91=9C=20=EC=83=9D=EC=84=B1=20api=20=EC=98=A4=EB=A5=98?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80=20-=EC=9E=90=EA=B0=80?= =?UTF-8?q?=EC=A7=84=EB=8B=A8=ED=91=9C=EC=9D=98=20visitType=20=ED=95=84?= =?UTF-8?q?=EB=93=9C=20=EC=9E=85=EB=A0=A5=EA=B0=92(enum)=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../code/error/CommonErrorCode.java | 2 +- .../apiPayload/handler/ExceptionAdvice.java | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) 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()) {