Skip to content

Commit 504b11d

Browse files
authored
Merge pull request #10 from FitTheMan/feature/#9
feat: API Response와 Exception 설정
2 parents 7be3f59 + 52bb62e commit 504b11d

File tree

8 files changed

+126
-1
lines changed

8 files changed

+126
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ out/
3434
/.nb-gradle/
3535

3636
### VS Code ###
37-
.vscode/
37+
.vscode/
38+
39+
### env ###
40+
.env

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

src/main/resources/application-datasource.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ spring:
1010
password: ${POSTGRES_PASSWORD}
1111

1212
jpa:
13+
database-platform: org.hibernate.dialect.PostgreSQLDialect
1314
properties:
1415
hibernate:
1516
default_batch_fetch_size: ${JPA_BATCH_FETCH_SIZE:100}

0 commit comments

Comments
 (0)