Skip to content

Commit

Permalink
refactor: ErrorCode 네이밍 변경 (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdkdhoho committed Feb 14, 2024
1 parent 110b1f1 commit 4dfd76a
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 32 deletions.
39 changes: 20 additions & 19 deletions src/main/java/com/listywave/common/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.listywave.common.exception;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;
Expand All @@ -8,30 +14,25 @@
@AllArgsConstructor
public enum ErrorCode {

// Common
METHOD_ARGUMENT_TYPE_MISMATCH(HttpStatus.BAD_REQUEST, "요청 한 값 타입이 잘못되어 binding에 실패하였습니다."),
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "지원하지 않는 HTTP method 입니다."),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 오류, 관리자에게 문의하세요"),
// Auth
NOT_SUPPORTED_OAUTH_SERVER(BAD_REQUEST, "지원하지 않는 OAuth 서버입니다."),
REQUIRED_ACCESS_TOKEN(UNAUTHORIZED, "인증 정보가 필요합니다."),
INVALID_ACCESS_TOKEN(UNAUTHORIZED, "유효하지 않은 AccessToken 입니다. 다시 로그인해주세요."),
INVALID_ACCESS(FORBIDDEN, "접근 권한이 존재하지 않습니다."),

NOT_SUPPORTED_OAUTH_SERVER(HttpStatus.BAD_REQUEST, "지원하지 않는 OAuth 서버입니다."),
// Http Request
METHOD_ARGUMENT_TYPE_MISMATCH(BAD_REQUEST, "요청 한 값 타입이 잘못되어 binding에 실패하였습니다."),
RESOURCE_NOT_FOUND(NOT_FOUND, "대상이 존재하지 않습니다."),

// Validation
NICKNAME_CONTAINS_WHITESPACE(HttpStatus.BAD_REQUEST, "닉네임의 처음과 마지막에 공백이 존재할 수 없습니다."),
NICKNAME_CONTAINS_SPECIAL_CHARACTERS(HttpStatus.BAD_REQUEST, "이모티콘 및 특수문자가 포함될 수 없습니다."),
LENGTH_EXCEEDED(HttpStatus.BAD_REQUEST, "최대 글자 길이를 초과하였습니다."),
INVALID_COUNT(HttpStatus.BAD_REQUEST, "선택한 아이템 및 라벨의 개수가 올바르지 않습니다."),
NOT_FOUND(HttpStatus.BAD_REQUEST, "대상이 존재하지 않습니다."),

REQUIRED_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "인증 정보가 필요합니다."),
INVALID_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 AccessToken 입니다. 다시 로그인해주세요."),

DUPLICATE_USER(HttpStatus.BAD_REQUEST, "중복된 사용자를 선택할 수 없습니다."),

// list
INVALID_ACCESS(HttpStatus.FORBIDDEN, "접근 권한이 존재하지 않습니다."),
NICKNAME_CONTAINS_WHITESPACE(BAD_REQUEST, "닉네임의 처음과 마지막에 공백이 존재할 수 없습니다."),
NICKNAME_CONTAINS_SPECIAL_CHARACTERS(BAD_REQUEST, "이모티콘 및 특수문자가 포함될 수 없습니다."),
LENGTH_EXCEEDED(BAD_REQUEST, "최대 글자 길이를 초과하였습니다."),
INVALID_COUNT(BAD_REQUEST, "선택한 아이템 및 라벨의 개수가 올바르지 않습니다."),
DUPLICATE_USER(BAD_REQUEST, "중복된 사용자를 선택할 수 없습니다."),

// S3
S3_DELETE_OBJECTS_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, "서버 오류, 관리자에게 문의하세요"),
S3_DELETE_OBJECTS_EXCEPTION(INTERNAL_SERVER_ERROR, "S3의 이미지를 삭제 요청하는 과정에서 에러가 발생했습니다."),
;

private final HttpStatus status;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/listywave/common/util/UserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public class UserUtil {
public User getUserByUserid(Long userId) {
return userRepository
.findById(userId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND, "해당 회원을 찾을 수 없습니다."));
.orElseThrow(() -> new CustomException(ErrorCode.RESOURCE_NOT_FOUND, "해당 회원을 찾을 수 없습니다."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public static ImageFileExtension fromString(String key) {
return Arrays.stream(ImageFileExtension.values())
.filter(extensionType -> extensionType.name().equalsIgnoreCase(key))
.findFirst()
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND, "해당 이미지 확장자가 존재하지 않습니다."));
.orElseThrow(() -> new CustomException(ErrorCode.RESOURCE_NOT_FOUND, "해당 이미지 확장자가 존재하지 않습니다."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ private void updateUserImageKey(User user, String profileImageKey, String backgr
private Item findItem(Long listId, int rank) {
return itemRepository
.findByListIdAndRanking(listId, rank)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND, "해당 아이템이 존재하지 않습니다."));
.orElseThrow(() -> new CustomException(ErrorCode.RESOURCE_NOT_FOUND, "해당 아이템이 존재하지 않습니다."));
}

private GeneratePresignedUrlRequest createGeneratePreSignedUrlRequest(
Expand Down Expand Up @@ -426,7 +426,7 @@ private void validateListUserMismatch(ListEntity list, User user) {
private ListEntity findListById(Long listId) {
return listRepository
.findById(listId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND, "존재하지 않는 리스트입니다."));
.orElseThrow(() -> new CustomException(ErrorCode.RESOURCE_NOT_FOUND, "존재하지 않는 리스트입니다."));
}

public String getCurrentProfile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public static CategoryType enumOf(String codeValue) {
return Arrays.stream(CategoryType.values())
.filter(t -> t.getCodeValue().equals(codeValue))
.findAny()
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND, "해당 카테고리코드는 존재하지 않습니다."));
.orElseThrow(() -> new CustomException(ErrorCode.RESOURCE_NOT_FOUND, "해당 카테고리코드는 존재하지 않습니다."));
}

@JsonCreator
public static CategoryType fromString(String key) {
return Arrays.stream(CategoryType.values())
.filter(categoryType -> categoryType.name().equalsIgnoreCase(key))
.findFirst()
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND, "해당 카테고리는 존재하지 않습니다."));
.orElseThrow(() -> new CustomException(ErrorCode.RESOURCE_NOT_FOUND, "해당 카테고리는 존재하지 않습니다."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private List<User> findExistingCollaborators(List<Long> collaboratorIds) {
.toList();

if (!nonExistingIds.isEmpty()) {
throw new CustomException(ErrorCode.NOT_FOUND, "콜라보레이터로 등록한 회원이 존재하지 않습니다.");
throw new CustomException(ErrorCode.RESOURCE_NOT_FOUND, "콜라보레이터로 등록한 회원이 존재하지 않습니다.");
}
return existingCollaborators;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.listywave.list.repository;

import static com.listywave.common.exception.ErrorCode.NOT_FOUND;
import static com.listywave.common.exception.ErrorCode.RESOURCE_NOT_FOUND;

import com.listywave.common.exception.CustomException;
import com.listywave.list.application.domain.Comment;
Expand All @@ -11,7 +11,7 @@
public interface CommentRepository extends JpaRepository<Comment, Long>, CustomCommentRepository {

default Comment getById(Long id) {
return findById(id).orElseThrow(() -> new CustomException(NOT_FOUND));
return findById(id).orElseThrow(() -> new CustomException(RESOURCE_NOT_FOUND));
}

List<Comment> findAllByList(ListEntity list);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.listywave.list.repository.list;

import static com.listywave.common.exception.ErrorCode.NOT_FOUND;
import static com.listywave.common.exception.ErrorCode.RESOURCE_NOT_FOUND;

import com.listywave.common.exception.CustomException;
import com.listywave.list.application.domain.ListEntity;
Expand All @@ -10,6 +10,6 @@
public interface ListRepository extends JpaRepository<ListEntity, Long>, CustomListRepository {

default ListEntity getById(Long listId) {
return findById(listId).orElseThrow(() -> new CustomException(NOT_FOUND));
return findById(listId).orElseThrow(() -> new CustomException(RESOURCE_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.listywave.user.repository.user;

import static com.listywave.common.exception.ErrorCode.NOT_FOUND;
import static com.listywave.common.exception.ErrorCode.RESOURCE_NOT_FOUND;

import com.listywave.common.exception.CustomException;
import com.listywave.user.application.domain.User;
Expand All @@ -13,7 +13,7 @@ public interface UserRepository extends JpaRepository<User, Long>, CustomUserRep
Optional<User> findByOauthId(Long oauthId);

default User getById(Long id) {
return findById(id).orElseThrow(() -> new CustomException(NOT_FOUND));
return findById(id).orElseThrow(() -> new CustomException(RESOURCE_NOT_FOUND));
}

Boolean existsByNicknameValue(String nickname);
Expand Down

0 comments on commit 4dfd76a

Please sign in to comment.