Skip to content

Commit 3bbca37

Browse files
authored
Merge pull request #137 from IT-Cotato/feature/136-refactor-error-code
Refactor : 커스텀 에러 코드 추가(#136)
2 parents 6207b29 + 9d5f91d commit 3bbca37

File tree

5 files changed

+76
-46
lines changed

5 files changed

+76
-46
lines changed

backend/src/main/java/middle_point_search/backend/common/dto/ErrorResponse.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,44 @@
1414
@JsonInclude(JsonInclude.Include.NON_NULL)
1515
public class ErrorResponse extends BaseResponse {
1616
private final String error;
17+
private final String code;
1718
private final List<String> reason;
1819

19-
private ErrorResponse(Boolean isSuccess, HttpStatusCode status, String error) {
20+
private ErrorResponse(Boolean isSuccess, HttpStatusCode status, String code, String error) {
2021
super(isSuccess, status);
2122
this.error = error;
23+
this.code = code;
2224
this.reason = null;
2325
}
2426

25-
public ErrorResponse(Boolean isSuccess, HttpStatusCode status, String error, List<String> reason) {
27+
public ErrorResponse(Boolean isSuccess, HttpStatusCode status, String code, String error, List<String> reason) {
2628
super(isSuccess, status);
2729
this.error = error;
30+
this.code = code;
2831
this.reason = reason;
2932
}
3033

31-
public static ErrorResponse of(HttpStatusCode status, String error) {
34+
public static ErrorResponse of(HttpStatusCode status, String code, String error) {
3235
Boolean isSuccess = false;
3336

34-
return new ErrorResponse(isSuccess, status, error);
37+
return new ErrorResponse(isSuccess, status, code, error);
3538
}
3639

3740
public static ErrorResponse of(ErrorCode errorCode, List<String> reason) {
3841
Boolean isSuccess = false;
3942
HttpStatus status = errorCode.getHttpStatus();
43+
String code = errorCode.getCode();
4044
String error = errorCode.getMessage();
4145

42-
return new ErrorResponse(isSuccess, status, error, reason);
46+
return new ErrorResponse(isSuccess, status, code, error, reason);
4347
}
4448

4549
public static ErrorResponse from(ErrorCode errorCode) {
4650
Boolean isSuccess = false;
4751
HttpStatus status = errorCode.getHttpStatus();
52+
String code = errorCode.getCode();
4853
String error = errorCode.getMessage();
4954

50-
return new ErrorResponse(isSuccess, status, error);
55+
return new ErrorResponse(isSuccess, status, code, error);
5156
}
52-
53-
54-
5557
}

backend/src/main/java/middle_point_search/backend/common/exception/errorCode/CommonErrorCode.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,32 @@
77
@Getter
88
public enum CommonErrorCode implements ErrorCode {
99

10-
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "인증에 실패하였습니다."),
11-
FORBIDDEN(HttpStatus.FORBIDDEN, "접근이 거부되었습니다."),
12-
BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."),
13-
EXTERNAL_SERVER_ERROR(HttpStatus.BAD_REQUEST, "외부 서버로의 요청이 실패하였습니다."),
14-
INVALID_PARAMETER(HttpStatus.BAD_REQUEST, "요청 파라미터가 잘 못 되었습니다."),
15-
RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "리소스를 찾을 수 없습니다."),
16-
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부에서 에러가 발생하였습니다."),
17-
DATA_BUFFER_LIMIT_ERROR(HttpStatus.PAYLOAD_TOO_LARGE, "요청의 페이로드가 너무 큽니다."),
18-
AUTHENTICATION_SERVICE_ERROR(HttpStatus.BAD_REQUEST, "요청한 content-type은 허락되지 않습니다."),
19-
TOO_MANY_REQUESTS(HttpStatus.TOO_MANY_REQUESTS, "요청을 너무 많이 했습니다."),
10+
// 1XX
11+
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "C-101", "인증에 실패하였습니다."),
12+
FORBIDDEN(HttpStatus.FORBIDDEN, "C-102", "접근이 거부되었습니다."),
13+
AUTHENTICATION_SERVICE_ERROR(HttpStatus.BAD_REQUEST, "C-103", "요청한 content-type은 허락되지 않습니다."),
14+
15+
// 2XX
16+
BAD_REQUEST(HttpStatus.BAD_REQUEST, "C-201", "잘못된 요청입니다."),
17+
INVALID_PARAMETER(HttpStatus.BAD_REQUEST, "C-202", "요청 파라미터가 잘 못 되었습니다."),
18+
TOO_MANY_REQUESTS(HttpStatus.TOO_MANY_REQUESTS, "C-203", "요청을 너무 많이 했습니다."),
19+
DATA_BUFFER_LIMIT_ERROR(HttpStatus.PAYLOAD_TOO_LARGE, "C-204", "요청의 페이로드가 너무 큽니다."),
20+
21+
// 3XX
22+
RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "C-301", "리소스를 찾을 수 없습니다."),
23+
24+
// 4XX
25+
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "C-401", "서버 내부에서 에러가 발생하였습니다."),
26+
EXTERNAL_SERVER_ERROR(HttpStatus.BAD_REQUEST, "C-402", "외부 서버로의 요청이 실패하였습니다.");
2027
;
2128

2229
private final HttpStatus httpStatus;
30+
private final String code;
2331
private final String message;
2432

25-
CommonErrorCode(HttpStatus httpStatus, String message) {
33+
CommonErrorCode(HttpStatus httpStatus, String code, String message) {
2634
this.httpStatus = httpStatus;
35+
this.code = code;
2736
this.message = message;
2837
}
2938

@@ -32,6 +41,11 @@ public HttpStatus getHttpStatus() {
3241
return this.httpStatus;
3342
}
3443

44+
@Override
45+
public String getCode() {
46+
return this.code;
47+
}
48+
3549
@Override
3650
public String getMessage() {
3751
return this.message;

backend/src/main/java/middle_point_search/backend/common/exception/errorCode/ErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
public interface ErrorCode {
66
HttpStatus getHttpStatus();
7+
String getCode();
78
String getMessage();
89
}

backend/src/main/java/middle_point_search/backend/common/exception/errorCode/UserErrorCode.java

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,54 @@
77
@Getter
88
public enum UserErrorCode implements ErrorCode {
99

10-
//4xx
11-
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "인증에 실패하였습니다."),
12-
INVALID_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "Access Token이 유효하지 않습니다."),
13-
INVALID_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "Refresh Token이 유효하지 않습니다."),
14-
REISSUE_ACCESS_TOKEN(HttpStatus.PAYMENT_REQUIRED, "Access Token을 재발급해야합니다."),
15-
ACCESS_DENIED(HttpStatus.FORBIDDEN, "접근 권한이 없습니다."),
16-
17-
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 회원입니다."),
18-
MEMBER_CREDENTIAL_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지 않습니다"),
19-
API_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "API 인증 정보가 정확하지 않습니다."),
20-
ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 방입니다."),
21-
PLACE_NOT_FOUND(HttpStatus.NOT_FOUND, "방에 입력된 장소가 없습니다."),
22-
PLACE_CONFLICT(HttpStatus.CONFLICT, "이미 장소를 저장하였습니다."),
23-
ROOM_TYPE_UNPROCESSABLE(HttpStatus.UNPROCESSABLE_ENTITY, "방의 타입이 일치하지 않습니다."),
24-
25-
VOTE_NOT_FOUND(HttpStatus.NOT_FOUND, "투표를 한 적이 없습니다."),
26-
VOTE_ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "생성된 투표방이 없습니다."),
27-
CANDIDATE_NOT_FOUND(HttpStatus.BAD_REQUEST, "투표 후보가 아닙니다."),
28-
ALREADY_VOTED(HttpStatus.CONFLICT, "이미 투표를 하였습니다."),
29-
DUPLICATE_VOTE_ROOM(HttpStatus.CONFLICT, "이미 투표방이 존재합니다."),
30-
31-
//5xx
32-
API_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "API 서버에 문제가 발생하였습니다.");
10+
//인증 및 토큰 관련
11+
UNAUTHORIZED(HttpStatus.UNAUTHORIZED,"A-001", "인증에 실패하였습니다."),
12+
INVALID_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED,"A-002", "Access Token이 유효하지 않습니다."),
13+
INVALID_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED,"A-003", "Refresh Token이 유효하지 않습니다."),
14+
REISSUE_ACCESS_TOKEN(HttpStatus.PAYMENT_REQUIRED,"A-004", "Access Token을 재발급해야합니다."),
15+
ACCESS_DENIED(HttpStatus.FORBIDDEN, "A-005", "접근 권한이 없습니다."),
16+
API_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "A-006", "API 인증 정보가 정확하지 않습니다."),
17+
18+
//회원 관련
19+
MEMBER_CREDENTIAL_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "M-002", "비밀번호가 일치하지 않습니다"),
20+
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "M-201", "존재하지 않는 회원입니다."),
21+
22+
//방 관련
23+
ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "R-201", "존재하지 않는 방입니다."),
24+
ROOM_TYPE_UNPROCESSABLE(HttpStatus.UNPROCESSABLE_ENTITY, "R-001", "방의 타입이 일치하지 않습니다."),
25+
26+
//장소 관련
27+
PLACE_NOT_FOUND(HttpStatus.NOT_FOUND, "P-201", "방에 입력된 장소가 없습니다."),
28+
PLACE_CONFLICT(HttpStatus.CONFLICT, "P-302", "이미 장소를 저장하였습니다."),
29+
30+
//트표 관련
31+
CANDIDATE_NOT_FOUND(HttpStatus.BAD_REQUEST, "V-101", "투표 후보가 아닙니다."),
32+
VOTE_NOT_FOUND(HttpStatus.NOT_FOUND, "V-201", "투표를 한 적이 없습니다."),
33+
VOTE_ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "V-202", "생성된 투표방이 없습니다."),
34+
ALREADY_VOTED(HttpStatus.CONFLICT, "V-301", "이미 투표를 하였습니다."),
35+
DUPLICATE_VOTE_ROOM(HttpStatus.CONFLICT, "V-302", "이미 투표방이 존재합니다."),
36+
37+
//서버 관련
38+
API_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "S-001", "API 서버에 문제가 발생하였습니다.");
3339

3440
private final HttpStatus httpStatus;
41+
private final String code;
3542
private final String message;
3643

37-
UserErrorCode(HttpStatus httpStatus, String message) {
44+
UserErrorCode(HttpStatus httpStatus, String code, String message) {
3845
this.httpStatus = httpStatus;
46+
this.code = code;
3947
this.message = message;
4048
}
4149

4250
@Override
4351
public HttpStatus getHttpStatus() {
4452
return this.httpStatus;
4553
}
54+
@Override
55+
public String getCode() {
56+
return this.code;
57+
}
4658

4759
@Override
4860
public String getMessage() {

backend/src/main/java/middle_point_search/backend/common/security/exception/hadlingFilter/ExceptionHandlingFilter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import jakarta.servlet.http.HttpServletRequest;
1111
import jakarta.servlet.http.HttpServletResponse;
1212
import lombok.extern.slf4j.Slf4j;
13-
import middle_point_search.backend.common.dto.DataResponse;
1413
import middle_point_search.backend.common.dto.ErrorResponse;
1514
import middle_point_search.backend.common.exception.CustomException;
15+
import middle_point_search.backend.common.exception.errorCode.CommonErrorCode;
1616
import middle_point_search.backend.common.util.ResponseWriter;
1717

1818
@Slf4j
@@ -30,7 +30,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
3030
} catch (Exception e) {
3131
log.warn("ExceptionHandlingFilter: {}", e.getMessage());
3232

33-
ErrorResponse errorResponse = ErrorResponse.of(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
33+
ErrorResponse errorResponse = ErrorResponse.from(CommonErrorCode.INTERNAL_SERVER_ERROR);
34+
3435
ResponseWriter.writeResponse(response, errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
3536
}
3637
}

0 commit comments

Comments
 (0)