Skip to content

Commit c257487

Browse files
committed
mod::response code int로 변경
1 parent 5b606c8 commit c257487

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

ontime-back/src/main/java/devkor/ontime_back/response/ApiResponseForm.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
public class ApiResponseForm<T> {
99
// 제네릭 api 응답 객체
1010
private String status;
11-
private String code;
11+
private int code;
1212
private String message;
1313
private final T data;
14-
public ApiResponseForm(String status, String code, String message, T data) {
14+
public ApiResponseForm(String status, int code, String message, T data) {
1515
this.status = status; // HttpResponse의 생성자 호출 (부모 클래스의 생성자 또는 메서드를 호출, 자식 클래스는 부모 클래스의 private 필드에 직접 접근 X)
1616
this.code = code;
1717
this.message = message;
@@ -20,33 +20,33 @@ public ApiResponseForm(String status, String code, String message, T data) {
2020

2121
// 성공 응답을 위한 메서드 (message를 받는 경우)
2222
public static <T> ApiResponseForm<T> success(T data, String message) {
23-
return new ApiResponseForm<>("success", "200", message, data);
23+
return new ApiResponseForm<>("success", 200, message, data);
2424
}
2525

2626
// 성공 응답을 위한 메서드 (message를 받지 않는 경우)
2727
public static <T> ApiResponseForm<T> success(T data) {
28-
return new ApiResponseForm<>("success", "200", "OK", data);
28+
return new ApiResponseForm<>("success", 200, "OK", data);
2929
}
3030

3131
// 실패 응답을 위한 메서드
32-
public static <T> ApiResponseForm<T> fail(String code, String message) {
32+
public static <T> ApiResponseForm<T> fail(int code, String message) {
3333
return new ApiResponseForm<>("fail", code, message, null); // 실패의 경우 data는 null로 처리
3434
}
3535

36-
public static <T> ApiResponseForm<T> accessTokenEmpty(String code, String message) {
36+
public static <T> ApiResponseForm<T> accessTokenEmpty(int code, String message) {
3737
return new ApiResponseForm<>("accessTokenEmpty", code, message, null); // 실패의 경우 data는 null로 처리
3838
}
3939

40-
public static <T> ApiResponseForm<T> accessTokenInvalid(String code, String message) {
40+
public static <T> ApiResponseForm<T> accessTokenInvalid(int code, String message) {
4141
return new ApiResponseForm<>("accessTokenInvalid", code, message, null); // 실패의 경우 data는 null로 처리
4242
}
4343

44-
public static <T> ApiResponseForm<T> refreshTokenInvalid(String code, String message) {
44+
public static <T> ApiResponseForm<T> refreshTokenInvalid(int code, String message) {
4545
return new ApiResponseForm<>("refreshTokenInvalid", code, message, null); // 실패의 경우 data는 null로 처리
4646
}
4747

4848
// 오류 응답을 위한 메서드
49-
public static <T> ApiResponseForm<T> error(String code, String message) {
49+
public static <T> ApiResponseForm<T> error(int code, String message) {
5050
return new ApiResponseForm<>("error", code, message, null); // 오류의 경우 data는 null로 처리
5151
}
5252

ontime-back/src/main/java/devkor/ontime_back/response/ErrorCode.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,47 @@
55
public enum ErrorCode {
66

77
// HTTP 상태 코드 (4xx)
8-
BAD_REQUEST("400", "Bad Request: Invalid input or malformed request.", HttpStatus.BAD_REQUEST),
9-
UNAUTHORIZED("401", "Unauthorized: You must authenticate to access this resource.", HttpStatus.UNAUTHORIZED),
10-
FORBIDDEN("403", "Forbidden: You do not have permission to access this resource.", HttpStatus.FORBIDDEN),
11-
NOT_FOUND("404", "Not Found: The requested resource could not be found.", HttpStatus.NOT_FOUND),
8+
BAD_REQUEST(400, "Bad Request: Invalid input or malformed request.", HttpStatus.BAD_REQUEST),
9+
UNAUTHORIZED(401, "Unauthorized: You must authenticate to access this resource.", HttpStatus.UNAUTHORIZED),
10+
FORBIDDEN(403, "Forbidden: You do not have permission to access this resource.", HttpStatus.FORBIDDEN),
11+
NOT_FOUND(404, "Not Found: The requested resource could not be found.", HttpStatus.NOT_FOUND),
1212

1313
// HTTP 상태 코드 (5xx)
14-
INTERNAL_SERVER_ERROR("500", "Internal Server Error: An unexpected error occurred on the server.", HttpStatus.INTERNAL_SERVER_ERROR),
15-
BAD_GATEWAY("502", "Bad Gateway: The server received an invalid response from the upstream server.", HttpStatus.BAD_GATEWAY),
16-
SERVICE_UNAVAILABLE("503", "Service Unavailable: The server is temporarily unable to handle the request.", HttpStatus.SERVICE_UNAVAILABLE),
14+
INTERNAL_SERVER_ERROR(500, "Internal Server Error: An unexpected error occurred on the server.", HttpStatus.INTERNAL_SERVER_ERROR),
15+
BAD_GATEWAY(502, "Bad Gateway: The server received an invalid response from the upstream server.", HttpStatus.BAD_GATEWAY),
16+
SERVICE_UNAVAILABLE(503, "Service Unavailable: The server is temporarily unable to handle the request.", HttpStatus.SERVICE_UNAVAILABLE),
1717

1818
// 비즈니스 로직 오류 코드
19-
USER_NOT_FOUND("1001", "해당 ID의 사용자를 찾을 수 없습니다.", HttpStatus.BAD_REQUEST),
20-
INVALID_INPUT("1002", "유효하지 않은 입력값입니다.", HttpStatus.BAD_REQUEST),
21-
RESOURCE_ALREADY_EXISTS("1003", "생성하려는 리소스가 이미 존재합니다.", HttpStatus.BAD_REQUEST),
22-
UNAUTHORIZED_ACCESS("1004", "해당 작업에 대한 권한이 없습니다.", HttpStatus.UNAUTHORIZED),
23-
EMAIL_ALREADY_EXIST("1005", "이미 존재하는 이메일입니다.", HttpStatus.BAD_REQUEST),
24-
NAME_ALREADY_EXIST("1006", "이미 존재하는 이름입니다.", HttpStatus.BAD_REQUEST),
25-
USER_SETTING_ALREADY_EXIST("1007", "이미 존재하는 userSettingId 입니다.", HttpStatus.BAD_REQUEST),
26-
PASSWORD_INCORRECT("1008", "기존 비밀번호가 틀렸습니다.", HttpStatus.BAD_REQUEST),
27-
SAME_PASSWORD("1009", "새 비밀번호와 기존 비밀번호가 일치합니다.", HttpStatus.BAD_REQUEST),
28-
SCHEDULE_NOT_FOUND("1010", "해당 약속이 존재하지 않습니다.", HttpStatus.BAD_REQUEST),
29-
FIREBASE("1011", "FIREBASE로 메세지를 발송하였으나 오류가 발생했습니다.(유효하지 않은 토큰 등)", HttpStatus.BAD_REQUEST),
30-
FIRST_PREPARATION_NOT_FOUND("1012", "해당 ID의 사용자의 준비과정을 찾을 수 없습니다.", HttpStatus.BAD_REQUEST),
31-
NOTIFICATION_NOT_FOUND("1013", "알림을 찾을 수 없습니다.", HttpStatus.BAD_REQUEST ),
19+
USER_NOT_FOUND(1001, "해당 ID의 사용자를 찾을 수 없습니다.", HttpStatus.BAD_REQUEST),
20+
INVALID_INPUT(1002, "유효하지 않은 입력값입니다.", HttpStatus.BAD_REQUEST),
21+
RESOURCE_ALREADY_EXISTS(1003, "생성하려는 리소스가 이미 존재합니다.", HttpStatus.BAD_REQUEST),
22+
UNAUTHORIZED_ACCESS(1004, "해당 작업에 대한 권한이 없습니다.", HttpStatus.UNAUTHORIZED),
23+
EMAIL_ALREADY_EXIST(1005, "이미 존재하는 이메일입니다.", HttpStatus.BAD_REQUEST),
24+
NAME_ALREADY_EXIST(1006, "이미 존재하는 이름입니다.", HttpStatus.BAD_REQUEST),
25+
USER_SETTING_ALREADY_EXIST(1007, "이미 존재하는 userSettingId 입니다.", HttpStatus.BAD_REQUEST),
26+
PASSWORD_INCORRECT(1008, "기존 비밀번호가 틀렸습니다.", HttpStatus.BAD_REQUEST),
27+
SAME_PASSWORD(1009, "새 비밀번호와 기존 비밀번호가 일치합니다.", HttpStatus.BAD_REQUEST),
28+
SCHEDULE_NOT_FOUND(1010, "해당 약속이 존재하지 않습니다.", HttpStatus.BAD_REQUEST),
29+
FIREBASE(1011, "FIREBASE로 메세지를 발송하였으나 오류가 발생했습니다.(유효하지 않은 토큰 등)", HttpStatus.BAD_REQUEST),
30+
FIRST_PREPARATION_NOT_FOUND(1012, "해당 ID의 사용자의 준비과정을 찾을 수 없습니다.", HttpStatus.BAD_REQUEST),
31+
NOTIFICATION_NOT_FOUND(1013, "알림을 찾을 수 없습니다.", HttpStatus.BAD_REQUEST ),
3232

3333
// 공통 오류 메시지
34-
UNEXPECTED_ERROR("1000", "Unexpected Error: An unexpected error occurred.", HttpStatus.INTERNAL_SERVER_ERROR),;
34+
UNEXPECTED_ERROR(1000, "Unexpected Error: An unexpected error occurred.", HttpStatus.INTERNAL_SERVER_ERROR),;
3535

36-
private final String code;
36+
private final int code;
3737
private final String message;
3838
private final HttpStatus httpStatus;
3939

4040
// 생성자
41-
ErrorCode(String code, String message, HttpStatus httpStatus) {
41+
ErrorCode(int code, String message, HttpStatus httpStatus) {
4242
this.code = code;
4343
this.message = message;
4444
this.httpStatus = httpStatus;
4545
}
4646

4747
// 코드와 메시지를 반환하는 메서드
48-
public String getCode() {
48+
public int getCode() {
4949
return code;
5050
}
5151

ontime-back/src/main/java/devkor/ontime_back/response/GlobalExceptionHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public ResponseEntity<ApiResponseForm<Void>> handleGeneralException(GeneralExcep
2525
public ResponseEntity<ApiResponseForm<Void>> handleInvalidTokenException(InvalidTokenException ex, HttpServletRequest request) {
2626
return ResponseEntity
2727
.status(HttpStatus.UNAUTHORIZED)
28-
.body(ApiResponseForm.error("401", ex.getMessage()));
28+
.body(ApiResponseForm.error(401, ex.getMessage()));
2929
}
3030

3131
@ExceptionHandler(HttpMessageNotReadableException.class)
@@ -34,7 +34,7 @@ public ResponseEntity<ApiResponseForm<Void>> handleHttpMessageNotReadableExcepti
3434
request.getRequestURI(), request.getMethod(), (request.getUserPrincipal() != null) ? request.getUserPrincipal().getName() : "Anonymous", request.getRemoteAddr(), "HttpMessageNotReadableException", "요청 형식이 올바르지 않습니다.", 400);
3535
return ResponseEntity
3636
.status(HttpStatus.BAD_REQUEST)
37-
.body(ApiResponseForm.error("400", "요청 형식이 올바르지 않습니다."));
37+
.body(ApiResponseForm.error(400, "요청 형식이 올바르지 않습니다."));
3838
}
3939

4040
}

0 commit comments

Comments
 (0)