Skip to content

Commit

Permalink
Merge pull request #137 from IT-Cotato/feature/136-refactor-error-code
Browse files Browse the repository at this point in the history
Refactor : 커스텀 에러 코드 추가(#136)
  • Loading branch information
yooooonshine authored Oct 9, 2024
2 parents 6207b29 + 9d5f91d commit 3bbca37
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,44 @@
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ErrorResponse extends BaseResponse {
private final String error;
private final String code;
private final List<String> reason;

private ErrorResponse(Boolean isSuccess, HttpStatusCode status, String error) {
private ErrorResponse(Boolean isSuccess, HttpStatusCode status, String code, String error) {
super(isSuccess, status);
this.error = error;
this.code = code;
this.reason = null;
}

public ErrorResponse(Boolean isSuccess, HttpStatusCode status, String error, List<String> reason) {
public ErrorResponse(Boolean isSuccess, HttpStatusCode status, String code, String error, List<String> reason) {
super(isSuccess, status);
this.error = error;
this.code = code;
this.reason = reason;
}

public static ErrorResponse of(HttpStatusCode status, String error) {
public static ErrorResponse of(HttpStatusCode status, String code, String error) {
Boolean isSuccess = false;

return new ErrorResponse(isSuccess, status, error);
return new ErrorResponse(isSuccess, status, code, error);
}

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

return new ErrorResponse(isSuccess, status, error, reason);
return new ErrorResponse(isSuccess, status, code, error, reason);
}

public static ErrorResponse from(ErrorCode errorCode) {
Boolean isSuccess = false;
HttpStatus status = errorCode.getHttpStatus();
String code = errorCode.getCode();
String error = errorCode.getMessage();

return new ErrorResponse(isSuccess, status, error);
return new ErrorResponse(isSuccess, status, code, error);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,32 @@
@Getter
public enum CommonErrorCode implements ErrorCode {

UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "인증에 실패하였습니다."),
FORBIDDEN(HttpStatus.FORBIDDEN, "접근이 거부되었습니다."),
BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."),
EXTERNAL_SERVER_ERROR(HttpStatus.BAD_REQUEST, "외부 서버로의 요청이 실패하였습니다."),
INVALID_PARAMETER(HttpStatus.BAD_REQUEST, "요청 파라미터가 잘 못 되었습니다."),
RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "리소스를 찾을 수 없습니다."),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부에서 에러가 발생하였습니다."),
DATA_BUFFER_LIMIT_ERROR(HttpStatus.PAYLOAD_TOO_LARGE, "요청의 페이로드가 너무 큽니다."),
AUTHENTICATION_SERVICE_ERROR(HttpStatus.BAD_REQUEST, "요청한 content-type은 허락되지 않습니다."),
TOO_MANY_REQUESTS(HttpStatus.TOO_MANY_REQUESTS, "요청을 너무 많이 했습니다."),
// 1XX
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "C-101", "인증에 실패하였습니다."),
FORBIDDEN(HttpStatus.FORBIDDEN, "C-102", "접근이 거부되었습니다."),
AUTHENTICATION_SERVICE_ERROR(HttpStatus.BAD_REQUEST, "C-103", "요청한 content-type은 허락되지 않습니다."),

// 2XX
BAD_REQUEST(HttpStatus.BAD_REQUEST, "C-201", "잘못된 요청입니다."),
INVALID_PARAMETER(HttpStatus.BAD_REQUEST, "C-202", "요청 파라미터가 잘 못 되었습니다."),
TOO_MANY_REQUESTS(HttpStatus.TOO_MANY_REQUESTS, "C-203", "요청을 너무 많이 했습니다."),
DATA_BUFFER_LIMIT_ERROR(HttpStatus.PAYLOAD_TOO_LARGE, "C-204", "요청의 페이로드가 너무 큽니다."),

// 3XX
RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "C-301", "리소스를 찾을 수 없습니다."),

// 4XX
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "C-401", "서버 내부에서 에러가 발생하였습니다."),
EXTERNAL_SERVER_ERROR(HttpStatus.BAD_REQUEST, "C-402", "외부 서버로의 요청이 실패하였습니다.");
;

private final HttpStatus httpStatus;
private final String code;
private final String message;

CommonErrorCode(HttpStatus httpStatus, String message) {
CommonErrorCode(HttpStatus httpStatus, String code, String message) {
this.httpStatus = httpStatus;
this.code = code;
this.message = message;
}

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

@Override
public String getCode() {
return this.code;
}

@Override
public String getMessage() {
return this.message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

public interface ErrorCode {
HttpStatus getHttpStatus();
String getCode();
String getMessage();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,54 @@
@Getter
public enum UserErrorCode implements ErrorCode {

//4xx
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "인증에 실패하였습니다."),
INVALID_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "Access Token이 유효하지 않습니다."),
INVALID_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "Refresh Token이 유효하지 않습니다."),
REISSUE_ACCESS_TOKEN(HttpStatus.PAYMENT_REQUIRED, "Access Token을 재발급해야합니다."),
ACCESS_DENIED(HttpStatus.FORBIDDEN, "접근 권한이 없습니다."),

MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 회원입니다."),
MEMBER_CREDENTIAL_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지 않습니다"),
API_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "API 인증 정보가 정확하지 않습니다."),
ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 방입니다."),
PLACE_NOT_FOUND(HttpStatus.NOT_FOUND, "방에 입력된 장소가 없습니다."),
PLACE_CONFLICT(HttpStatus.CONFLICT, "이미 장소를 저장하였습니다."),
ROOM_TYPE_UNPROCESSABLE(HttpStatus.UNPROCESSABLE_ENTITY, "방의 타입이 일치하지 않습니다."),

VOTE_NOT_FOUND(HttpStatus.NOT_FOUND, "투표를 한 적이 없습니다."),
VOTE_ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "생성된 투표방이 없습니다."),
CANDIDATE_NOT_FOUND(HttpStatus.BAD_REQUEST, "투표 후보가 아닙니다."),
ALREADY_VOTED(HttpStatus.CONFLICT, "이미 투표를 하였습니다."),
DUPLICATE_VOTE_ROOM(HttpStatus.CONFLICT, "이미 투표방이 존재합니다."),

//5xx
API_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "API 서버에 문제가 발생하였습니다.");
//인증 및 토큰 관련
UNAUTHORIZED(HttpStatus.UNAUTHORIZED,"A-001", "인증에 실패하였습니다."),
INVALID_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED,"A-002", "Access Token이 유효하지 않습니다."),
INVALID_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED,"A-003", "Refresh Token이 유효하지 않습니다."),
REISSUE_ACCESS_TOKEN(HttpStatus.PAYMENT_REQUIRED,"A-004", "Access Token을 재발급해야합니다."),
ACCESS_DENIED(HttpStatus.FORBIDDEN, "A-005", "접근 권한이 없습니다."),
API_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "A-006", "API 인증 정보가 정확하지 않습니다."),

//회원 관련
MEMBER_CREDENTIAL_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "M-002", "비밀번호가 일치하지 않습니다"),
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "M-201", "존재하지 않는 회원입니다."),

//방 관련
ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "R-201", "존재하지 않는 방입니다."),
ROOM_TYPE_UNPROCESSABLE(HttpStatus.UNPROCESSABLE_ENTITY, "R-001", "방의 타입이 일치하지 않습니다."),

//장소 관련
PLACE_NOT_FOUND(HttpStatus.NOT_FOUND, "P-201", "방에 입력된 장소가 없습니다."),
PLACE_CONFLICT(HttpStatus.CONFLICT, "P-302", "이미 장소를 저장하였습니다."),

//트표 관련
CANDIDATE_NOT_FOUND(HttpStatus.BAD_REQUEST, "V-101", "투표 후보가 아닙니다."),
VOTE_NOT_FOUND(HttpStatus.NOT_FOUND, "V-201", "투표를 한 적이 없습니다."),
VOTE_ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "V-202", "생성된 투표방이 없습니다."),
ALREADY_VOTED(HttpStatus.CONFLICT, "V-301", "이미 투표를 하였습니다."),
DUPLICATE_VOTE_ROOM(HttpStatus.CONFLICT, "V-302", "이미 투표방이 존재합니다."),

//서버 관련
API_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "S-001", "API 서버에 문제가 발생하였습니다.");

private final HttpStatus httpStatus;
private final String code;
private final String message;

UserErrorCode(HttpStatus httpStatus, String message) {
UserErrorCode(HttpStatus httpStatus, String code, String message) {
this.httpStatus = httpStatus;
this.code = code;
this.message = message;
}

@Override
public HttpStatus getHttpStatus() {
return this.httpStatus;
}
@Override
public String getCode() {
return this.code;
}

@Override
public String getMessage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import middle_point_search.backend.common.dto.DataResponse;
import middle_point_search.backend.common.dto.ErrorResponse;
import middle_point_search.backend.common.exception.CustomException;
import middle_point_search.backend.common.exception.errorCode.CommonErrorCode;
import middle_point_search.backend.common.util.ResponseWriter;

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

ErrorResponse errorResponse = ErrorResponse.of(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
ErrorResponse errorResponse = ErrorResponse.from(CommonErrorCode.INTERNAL_SERVER_ERROR);

ResponseWriter.writeResponse(response, errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Expand Down

0 comments on commit 3bbca37

Please sign in to comment.