-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BSVR-63] custom exception & global exception handler 추가 #15
Conversation
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public interface ErrorCode { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
모든 도메인의 errorCode가 하나의 enum class에서 관리되면, 클래스가 너무 커져서 관리하기 힘들거라고 생각했어요.
(e.g., 200개의 error code 중에서 member와 관련된 error들을 찾는 상상..)
따라서 공통 포맷을 인터페이스로 만들고, 도메인별로 errorCode를 구현하게 처리해 응집도를 높였어요.
import lombok.Getter; | ||
|
||
@Getter | ||
public enum MemberErrorCode implements ErrorCode { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
custom error를 만들어야 한다면, 이런식으로 ErrorCode를 구현하는 enum 클래스를 새로 생성하면 돼요.
public MemberErrorCode appended(Object o) { | ||
message = message + " {" + o.toString() + "}"; | ||
return this; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
정해진 메세지 외에 추가로 정보를 제공하고 싶을 수 있어요. 그때 사용하는 메서드에요.
public MemberNotFoundException() { | ||
super(MemberErrorCode.MEMBER_NOT_FOUND); | ||
} | ||
|
||
public MemberNotFoundException(Object o) { | ||
super(MemberErrorCode.MEMBER_NOT_FOUND.appended(o)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
단순 예시..!
no args constructor를 사용하면 enum code에 지정해둔 message가 할당될 것이고,
appended() 메서드를 활용하면 원하는 정보를 추가로 전달할 수 있어요.
(e.g., 특정 name에 해당하는 유저가 없을 경우, name : {string}을 message에 추가하는 등)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
📌 개요 (필수)
🔨 작업 사항 (필수)
🌱 연관 내용 (선택)
💻 실행 화면 (필수)