Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ 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", "인증이 필요합니다."),
_FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "금지된 요청입니다."),
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", "닉네임은 필수 입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -53,6 +55,28 @@ public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotVali
return handleExceptionInternal(errorCode, getDefaultMessage(e));
}

@Override
public ResponseEntity<Object> 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()) {
Expand Down