Skip to content

Commit

Permalink
Merge pull request #9 from ne-o-rdinary/refactor/manageError
Browse files Browse the repository at this point in the history
refactor : 에러 관리
  • Loading branch information
OZIIJIN authored Nov 23, 2024
2 parents c8c60ee + 9365d12 commit b279d78
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/example/demo/base/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ public static <T> ApiResponse<T> of(BaseCode code, T result) {
public static <T> ApiResponse<T> onFailure(String code, String message, T data) {
return new ApiResponse<>(false, code, message, data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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", "파일 업로드 실패"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,4 +96,17 @@ private ResponseEntity<ApiResponse<Object>> buildErrorResponse(ErrorReasonDTO er
ApiResponse<Object> body = ApiResponse.onFailure(errorReason.getCode(), errorReason.getMessage(), null);
return ResponseEntity.status(errorReason.getHttpStatus()).body(body);
}

@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<ApiResponse<?>> handleHttpMessageNotReadable(
HttpMessageNotReadableException ex) {
String errorMessage = "유효하지 않은 요청 형식입니다. ID는 숫자만 입력해야 합니다.";

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponse.onFailure(
"COMMON400",
errorMessage,
null
));
}
}
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit b279d78

Please sign in to comment.