Skip to content

Commit 7172ec9

Browse files
committed
feat: API Response와 Exception 설정
- api 공통 응답 정의 - api response code enum 정의 - Exception 설정
1 parent 7be3f59 commit 7172ec9

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

src/main/java/com/ftm/server/common/exception/.gitkeep

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.ftm.server.common.exception;
2+
3+
import com.ftm.server.common.response.enums.ErrorResponseCode;
4+
import lombok.Getter;
5+
6+
@Getter
7+
public class CustomException extends RuntimeException{
8+
9+
private final ErrorResponseCode errorResponseCode;
10+
11+
public CustomException(ErrorResponseCode errorResponseCode) {
12+
super(errorResponseCode.getMessage()); // 예외 메시지 설정
13+
this.errorResponseCode = errorResponseCode;
14+
}
15+
16+
public static CustomException USER_NOT_FOUND =
17+
new CustomException(ErrorResponseCode.USER_NOT_FOUND);
18+
19+
public static CustomException USER_ALREADY_EXISTS =
20+
new CustomException(ErrorResponseCode.USER_ALREADY_EXISTS);
21+
}

src/main/java/com/ftm/server/common/response/.gitkeep

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.ftm.server.common.response;
2+
3+
import com.ftm.server.common.response.enums.ErrorResponseCode;
4+
import com.ftm.server.common.response.enums.SuccessResponseCode;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Getter;
7+
8+
@Getter
9+
@AllArgsConstructor
10+
public class ApiResponse<T> {
11+
12+
private final Integer status;
13+
private final String code;
14+
private final String message;
15+
private final T data;
16+
17+
18+
public static <T> ApiResponse<T> success(SuccessResponseCode responseCode) {
19+
return success(responseCode,null);
20+
}
21+
22+
public static <T> ApiResponse<T> success(SuccessResponseCode responseCode, T data) {
23+
return new ApiResponse<>(responseCode.getHttpStatus().value(),responseCode.getCode(),responseCode.getMessage(),data);
24+
}
25+
26+
public static <T> ApiResponse<T> fail(ErrorResponseCode responseCode) {
27+
return fail(responseCode,null);
28+
}
29+
30+
public static <T> ApiResponse<T> fail(ErrorResponseCode responseCode, T data) {
31+
return new ApiResponse<>(responseCode.getHttpStatus().value(),responseCode.getCode(),responseCode.getMessage(),data);
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.ftm.server.common.response.enums;
2+
3+
import lombok.Getter;
4+
import org.springframework.http.HttpStatus;
5+
6+
@Getter
7+
public enum ErrorResponseCode {
8+
9+
//400번
10+
INVALID_REQUEST_ARGUMENT(HttpStatus.BAD_REQUEST,"E400_001","클라이언트 요청이 잘못된 형식이거나, 필수 데이터가 누락되었습니다."),
11+
12+
//401번
13+
NOT_AUTHENTICATED(HttpStatus.UNAUTHORIZED,"E401_001","인증되지 않은 사용자입니다."),
14+
15+
//403번
16+
NOT_AUTHORIZATION(HttpStatus.FORBIDDEN,"E403_001","인증된 사용자이나 해당 자원에 대한 접근 권한이 없습니다."),
17+
18+
//404번
19+
USER_NOT_FOUND(HttpStatus.NOT_FOUND,"E404_001","요청된 사용자를 찾을 수 없습니다."),
20+
21+
//409번
22+
USER_ALREADY_EXISTS(HttpStatus.CONFLICT, "E409_001","이미 존재하는 사용자입니다."),
23+
PASSWORD_NOT_MATCHED(HttpStatus.CONFLICT,"E409_002","비밀번호가 일치하지 않습니다."),
24+
25+
//500번
26+
UNKNOWN_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR,"E500_001","알 수 없는 서버 에러가 발생했습니다.");
27+
28+
private final HttpStatus httpStatus;
29+
private final String code;
30+
private final String message;
31+
32+
ErrorResponseCode(HttpStatus httpStatus, String code, String message){
33+
this.httpStatus = httpStatus;
34+
this.code = code;
35+
this.message = message;
36+
}
37+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.ftm.server.common.response.enums;
2+
3+
import lombok.Getter;
4+
import org.springframework.http.HttpStatus;
5+
6+
@Getter
7+
public enum SuccessResponseCode {
8+
9+
OK(HttpStatus.OK, "S001", "클라이언트 요청이 정상적으로 처리됨."),
10+
CREATED(HttpStatus.CREATED,"S002", "새로운 리소스가 성공적으로 생성됨."),
11+
ACCEPTED(HttpStatus.ACCEPTED,"S003","요청은 수락되었지만 아직 처리가 완료되지 않음."),
12+
NO_CONTENT(HttpStatus.NO_CONTENT,"S004", "요청이 성공적으로 처리되었지만, 응답 본문이 필요 없음.");
13+
14+
private final HttpStatus httpStatus;
15+
private final String code;
16+
private final String message;
17+
18+
SuccessResponseCode(HttpStatus httpStatus, String code, String message){
19+
this.httpStatus = httpStatus;
20+
this.code = code;
21+
this.message = message;
22+
}
23+
}

0 commit comments

Comments
 (0)