Skip to content

[Refactor] global handler exception 추가#7

Merged
yeonju73 merged 9 commits intomainfrom
refactor/#6/Global-Handler-Exception
Jul 9, 2025
Merged

[Refactor] global handler exception 추가#7
yeonju73 merged 9 commits intomainfrom
refactor/#6/Global-Handler-Exception

Conversation

@chaeyuuu
Copy link
Copy Markdown
Contributor

@chaeyuuu chaeyuuu commented Jul 9, 2025

💻 Related Issue

closed #6


🚀 Work Description

  • Global Handler Exception 적용
image
  • ErrorCode, SuccessCode : error code와 success code를 enum으로 정의
  • BaseResponse : 기본 응답 구조 정의 ex) code, msg, data
  • ErrorResponse : 에러 응답 구조 정의

Global Exception Handler 이 링크를 참고해서 작성하였습니다.

  • 스웨거 에러 문서화 추가
image
  • 사진과 같이 성공 예시 응답 코드와 함께 각 코드 별 에러 코드 추가

  • SwaggerResponseDescription : 발생 에러 코드 정의 < 하나의 기능에 발생하는 에러 코드들을 묶어서 표시 & 공통 에러 추가 -> api 에 @CustomExceptionDescription(SwaggerResponseDescription.REGISTER_FAIL) 이런식으로 추가하면 관련 에러 응답 & 공통 에러 응답 예시 뜸


🙇🏻‍♀️ To Reviewer

우선 스웨거 관련 코드들은 config > swagger 로 옮겨놨는데 필요하다면 패키지 구조 정리하면 될 것 같습니다.!
그리고 우선 예외 처리 및 스웨거 문서화 하였는데 구조 바꾸거나 이상한 부분 있으면 말씀해주세요!! 저도 처음해보는거라 헷갈리네요 ㅠ ㅠ
추가로 이전 pr에서 남겨주신 개선 사항들 적용하였습니다!

+) error code에 예외 값 추가한 다음에 service 로직에서 사용하면됩니다. 그리고 스웨거에 연동하기 위해서는 SwaggerResponseDescription에 추가해서 api에 @CustomExceptionDescription 붙여서 사용하시면 됩니다. 혹시 이상하거나 헷갈리는 부분 있으시면 말씀해주세요!

@chaeyuuu chaeyuuu self-assigned this Jul 9, 2025
Copy link
Copy Markdown
Contributor

@yeonju73 yeonju73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아이고~ 수고하셨습니다~~ 저도 잘 형식 맞춰서 해보겠습니다

Comment on lines +9 to +31
@Getter
public enum SwaggerResponseDescription {
REGISTER_FAIL(new LinkedHashSet<>(Set.of(
ErrorCode.DUPLICATED_EMAIL
))),

LOGIN_FAIL(new LinkedHashSet<>(Set.of(
ErrorCode.INVALID_EMAIL,
ErrorCode.INVALID_PASSWORD
)));

private final Set<ErrorCode> errorCodeList;

SwaggerResponseDescription(Set<ErrorCode> errorCodes) {
// 공통 에러 추가
errorCodes.addAll(Set.of(
ErrorCode.INVALID_TOKEN,
ErrorCode.INTERNAL_SERVER_ERROR
));

this.errorCodeList = errorCodes;
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인했습니다! 잘 참고해서 써보겠습니닷~~

Comment on lines +9 to +23
public enum ErrorCode {
/// 4000 ~ : client error
DUPLICATED_EMAIL(HttpStatus.BAD_REQUEST, 4001, "이미 가입된 이메일입니다."),
INVALID_EMAIL(HttpStatus.BAD_REQUEST, 4002, "존재하지 않는 이메일입니다."),
INVALID_PASSWORD(HttpStatus.BAD_REQUEST, 4003, "비밀번호가 일치하지 않습니다."),
INVALID_TOKEN(HttpStatus.UNAUTHORIZED, 4004, "유효하지 않은 토큰입니다."),

// 5000~ : server error
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, 5000, "서버 오류가 발생했습니다.");

private final HttpStatus status;
private final int code;
private final String msg;

}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ErrorCode 넘버링은 그냥 구현한 순서대로 하면될까요?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵!! 그런거같아요...

Comment on lines +8 to +12
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomExceptionDescription {
SwaggerResponseDescription value();
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오오~~ 신기하네요 커스텀 어노테이션,,,

@yeonju73 yeonju73 merged commit 59e358d into main Jul 9, 2025
@chaeyuuu chaeyuuu deleted the refactor/#6/Global-Handler-Exception branch July 12, 2025 03:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Refactor] Global Exception Handler 추가

2 participants