Skip to content

Commit 5dc474f

Browse files
committed
feat: 공통 예외 응답 클래스(ApiErrorResponse) 및 에러 코드 기반 구조 생성
1 parent ec1a1cd commit 5dc474f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.log4u.common.exception;
2+
3+
import java.util.List;
4+
5+
import org.springframework.validation.FieldError;
6+
7+
import com.fasterxml.jackson.annotation.JsonInclude;
8+
9+
import lombok.Builder;
10+
import lombok.Getter;
11+
import lombok.RequiredArgsConstructor;
12+
13+
@Getter
14+
@Builder
15+
@RequiredArgsConstructor
16+
public class ApiErrorResponse {
17+
private final String errorMessage;
18+
private final int errorCode;
19+
20+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
21+
private final List<ValidationError> errors;
22+
23+
public record ValidationError(String field, String message) {
24+
25+
public static ValidationError of(final FieldError fieldError) {
26+
return new ValidationError(fieldError.getField(), fieldError.getDefaultMessage());
27+
}
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example.log4u.common.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
5+
import com.example.log4u.common.exception.base.ErrorCode;
6+
7+
import lombok.Getter;
8+
import lombok.RequiredArgsConstructor;
9+
10+
@Getter
11+
@RequiredArgsConstructor
12+
public enum CommonErrorCode implements ErrorCode {
13+
14+
INVALID_PARAMETER(HttpStatus.BAD_REQUEST, "잘못된 요청입니다"),
15+
UNAUTHENTICATED(HttpStatus.UNAUTHORIZED,"로그인이 필요한 기능입니다."),
16+
FORBIDDEN(HttpStatus.FORBIDDEN, "접근 권한이 없습니다"),
17+
RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "요청 정보를 찾을 수 없습니다"),
18+
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부 오류입니다. 관리자에게 문의하세요.")
19+
;
20+
21+
private final HttpStatus httpStatus;
22+
private final String message;
23+
24+
@Override
25+
public HttpStatus getHttpStatus() {
26+
return this.httpStatus;
27+
}
28+
29+
@Override
30+
public String getErrorMessage() {
31+
return this.message;
32+
}
33+
}

0 commit comments

Comments
 (0)