From 9365d12e22e137823c28996f232b6d2454ac693a Mon Sep 17 00:00:00 2001 From: OZIIJIN Date: Sun, 24 Nov 2024 05:01:23 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EC=97=90=EB=9F=AC=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/demo/base/ApiResponse.java | 1 + .../base/code/status/exception/ErrorStatus.java | 1 + .../code/status/exception/ExceptionAdvice.java | 14 ++++++++++++++ .../demo/domain/dto/answer/AnswerRequestDto.java | 5 +++++ 4 files changed, 21 insertions(+) diff --git a/src/main/java/com/example/demo/base/ApiResponse.java b/src/main/java/com/example/demo/base/ApiResponse.java index d9dcf15..62def65 100644 --- a/src/main/java/com/example/demo/base/ApiResponse.java +++ b/src/main/java/com/example/demo/base/ApiResponse.java @@ -34,4 +34,5 @@ public static ApiResponse of(BaseCode code, T result) { public static ApiResponse onFailure(String code, String message, T data) { return new ApiResponse<>(false, code, message, data); } + } \ No newline at end of file diff --git a/src/main/java/com/example/demo/base/code/status/exception/ErrorStatus.java b/src/main/java/com/example/demo/base/code/status/exception/ErrorStatus.java index 3d9f461..89b88ec 100644 --- a/src/main/java/com/example/demo/base/code/status/exception/ErrorStatus.java +++ b/src/main/java/com/example/demo/base/code/status/exception/ErrorStatus.java @@ -15,6 +15,7 @@ public enum ErrorStatus implements BaseErrorCode { BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON400", "잘못된 요청입니다."), UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "COMMON401", "인증이 필요합니다."), FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "접근이 금지되었습니다."), + INVALID_ID_TYPE(HttpStatus.BAD_REQUEST, "COMMON400", "적합하지 않은 ID 타입입니다"), // s3 관련 에러 FILE_UPLOAD_FAILED(HttpStatus.INTERNAL_SERVER_ERROR, "FILE4001", "파일 업로드 실패"), diff --git a/src/main/java/com/example/demo/base/code/status/exception/ExceptionAdvice.java b/src/main/java/com/example/demo/base/code/status/exception/ExceptionAdvice.java index 3748f7b..93ef014 100644 --- a/src/main/java/com/example/demo/base/code/status/exception/ExceptionAdvice.java +++ b/src/main/java/com/example/demo/base/code/status/exception/ExceptionAdvice.java @@ -8,6 +8,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -95,4 +96,17 @@ private ResponseEntity> buildErrorResponse(ErrorReasonDTO er ApiResponse body = ApiResponse.onFailure(errorReason.getCode(), errorReason.getMessage(), null); return ResponseEntity.status(errorReason.getHttpStatus()).body(body); } + + @ExceptionHandler(HttpMessageNotReadableException.class) + public ResponseEntity> handleHttpMessageNotReadable( + HttpMessageNotReadableException ex) { + String errorMessage = "유효하지 않은 요청 형식입니다. ID는 숫자만 입력해야 합니다."; + + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ApiResponse.onFailure( + "COMMON400", + errorMessage, + null + )); + } } \ No newline at end of file diff --git a/src/main/java/com/example/demo/domain/dto/answer/AnswerRequestDto.java b/src/main/java/com/example/demo/domain/dto/answer/AnswerRequestDto.java index 9fe89c3..e200a18 100644 --- a/src/main/java/com/example/demo/domain/dto/answer/AnswerRequestDto.java +++ b/src/main/java/com/example/demo/domain/dto/answer/AnswerRequestDto.java @@ -1,15 +1,20 @@ package com.example.demo.domain.dto.answer; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Pattern; import lombok.Getter; @Getter public class AnswerRequestDto { @NotNull + @Min(value = 0, message = "적합하지 않은 ID 타입입니다.") private Long questionId; @NotNull + @Pattern(regexp = "^[^0-9]+$", message = "답변은 문자만 입력 가능합니다.") private String answer; public AnswerRequestDto(Long questionId, String answer) {