Skip to content

Commit 786f661

Browse files
authored
[infra] 커스텀 예외 구성
* infra: 커스텀 예외 구성 - ControllerAdvice 구현 - CustomException 구현 - 에러 코드 구현 및 에러 응답 객체 구현 * style: 어노테이션 정렬
1 parent 7fc7892 commit 786f661

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dmu.dasom.api.domain.common.exception;
2+
3+
import org.springframework.web.bind.annotation.ExceptionHandler;
4+
import org.springframework.web.bind.annotation.RestControllerAdvice;
5+
6+
@RestControllerAdvice
7+
public class CustomControllerAdvice {
8+
9+
@ExceptionHandler(CustomException.class)
10+
public ErrorResponse customException(final CustomException e) {
11+
return new ErrorResponse(e.getErrorCode().getCode(), e.getErrorCode().getMessage());
12+
}
13+
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package dmu.dasom.api.domain.common.exception;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class CustomException extends RuntimeException {
7+
8+
private final ErrorCode errorCode;
9+
10+
public CustomException(final ErrorCode errorCode) {
11+
super(errorCode.getMessage());
12+
this.errorCode = errorCode;
13+
}
14+
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dmu.dasom.api.domain.common.exception;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
@AllArgsConstructor
7+
@Getter
8+
public enum ErrorCode {
9+
10+
UNAUTHORIZED(401, "C001", "인증되지 않은 사용자입니다."),
11+
FORBIDDEN(403, "C002", "권한이 없습니다."),
12+
;
13+
14+
private final int status;
15+
private final String code;
16+
private final String message;
17+
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package dmu.dasom.api.domain.common.exception;
2+
3+
import lombok.AccessLevel;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
8+
@AllArgsConstructor
9+
@Getter
10+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
11+
public class ErrorResponse {
12+
13+
private String code;
14+
private String message;
15+
16+
}

0 commit comments

Comments
 (0)