Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor : 에러 관리 #9

Merged
merged 1 commit into from
Nov 23, 2024
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
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
Loading