안녕하세요.
Response에 내려오는 에러코드를 Enum으로 관리하는 방법을 공부해보고 정리한 글입니다.
Enum은 Eumeration로 열거형입니다.
열거형은 서로 연관된 상수들의 집합을 의미합니다.
기존에 상수를 정의하는 방법이였던 final static string 과 같이 문자열이나 숫자들을 나타내는 기본자료형의 값을 enum을 이용해서 같은 효과를 낼 수 있습니다.
메소드 | 설명 |
---|---|
static E values() | 해당 열거체의 모든 상수를 저장한 배열을 생성하여 반환함. |
static E valueOf(String name) | 전달된 문자열과 일치하는 해당 열거체의 상수를 반환함. |
protected void finalize() | 해당 Enum 클래스가 final 메소드를 가질 수 없게 됨. |
String name() | 해당 열거체 상수의 이름을 반환함. |
int ordinal() | 해당 열거체 상수가 열거체 정의에서 정의된 순서(0부터 시작)를 반환함. |
@Getter
public enum ErrorCode {
INVALID_PARAMS(400, "InvalidParams", "필수데이터 누락, 또는 형식과 다른 데이터를 요청하셨습니다.")
, UNAUTORIZED(401, "Unauthorized", "토큰 정보가 유효하지 않습니다.")
, UNAVAILABLE(401, "Unavailable", "회원가입이 완료되지 않은 사용자입니다.")
, NOT_FOUND(404, "NotFound", "존재하지 않는 데이터입니다.")
, CONFLICT(409, "Conflict", "데이터가 충돌되었습니다.")
;
ErrorCode (int status, , String code, String message) {
this.status = status;
this.code = code;
this.message = message;
}
private int status;
private String code;
private String message;
public static ErrorCode valueOfCode(String errorCode) {
return Arrays.stream(values())
.filter(value -> value.code.equals(errorCode))
.findAny()
.orElse(null);
}
}
코드 | 상태 코드 | 에러 메세지 |
---|---|---|
InvalidParams | 400 | 필수데이터 누락, 또는 형식과 다른 데이터를 요청하셨습니다. |
Unauthorized | 401 | 토큰 정보가 유효하지 않습니다. |
Unavailable | 401 | 회원가입이 완료되지 않은 사용자입니다. |
NotFound | 404 | 존재하지 않는 데이터입니다. |
Conflict | 409 | 데이터가 충돌되었습니다. |
참고
[Java] Enum의 사용법
[에러처리101] Enum으로 에러코드 관리하기
TCP SCHOOL Enum 클래스
I’m inspired with the surpassing and preachy listing that you furnish in such little timing. Visit Profile
Outstanding submit, travelled in advance and also book marked your internet site. My partner and i can’t hold out to learn a lot more coming from an individual. fotel uszak
Great Publish, I'm a large believer within publishing remarks upon websites in order to allow weblog authors realize that they’ve additional some thing beneficial in order to the internet! https://pdtgeneve.ch/lib/inc/le-code-promo-1xbet_bonus.html
This can be a excellent ideas particularly in order to individuals a new comer to blogosphere, short as well as precise information… Many thanks with regard to discussing that one. Essential study post. pokój dla dziewczynki 6 lat
That is great submit and also i spend time you just read this kind of submit. your blog will be great and you also have got excellent employees within your website. great revealing keep it up. 명품 편집샵
This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. 노래방알바
Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. 1 вин промокод
Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. code promo 1xbet lors de l'inscription
Essentially I actually learn them a short while ago nonetheless I had put together quite a few opinions regarding this now Needed to read simple things them just as before for the reason that it is well crafted. grandjp
That is the amazing piece of writing, Thanks a lot for the purpose of rendering everybody this. Have post. sbobetcash
I found this is an informative and interesting post so i think so it is very useful and knowledgeable. I would like to thank you for the efforts you have made in writing this article. https://fluentcpp.com/news/code_promo-1win-bonus_de_500.html
I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.익스피디아 쿠폰
I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.호텔스닷컴 쿠폰
Plan and book your entire trip with Trip.com, covering hotels, flights, tours, train tickets, and car rentals. This comprehensive platform allows easy management of all your reservations. Trust Trip.com for a seamless travel experience, ensuring all your needs are met.
Continue the great function, We study couple of articles about this web site as well as I believe that the internet weblog is actually actual fascinating and it has obtained groups associated with wonderful info.
I’m stirred along with the surpassing and additionally preachy list of you ought to produce such smaller timing.dabwoods vape
I did experience checking articles or reviews shared here. They are simply exceptional there are a large amount of advantageous knowledge. 양산호스트바