From 6b762251cccea84a734253ef1bd83861fbde7393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=9A=A9=EC=88=98?= Date: Thu, 19 Sep 2024 11:19:07 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20=EC=97=90=EB=9F=AC=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/controller/AuthController.java | 4 +-- .../domain/auth/service/AuthService.java | 2 +- .../domain/auth/service/JwtTokenService.java | 5 +-- .../comment/controller/CommentController.java | 2 +- .../comment/service/CommentService.java | 12 +++---- .../domain/folder/service/FolderService.java | 4 +-- .../image/controller/ImageController.java | 6 ++-- .../domain/image/service/ImageService.java | 7 ++-- .../domain/review/service/ReviewService.java | 8 ++--- .../worlabel/global/exception/ErrorCode.java | 33 +++++++------------ .../filter/JwtAuthenticationFilter.java | 1 - .../global/service/S3UploadService.java | 2 +- 12 files changed, 33 insertions(+), 53 deletions(-) diff --git a/backend/src/main/java/com/worlabel/domain/auth/controller/AuthController.java b/backend/src/main/java/com/worlabel/domain/auth/controller/AuthController.java index ba9e9b0..8abf4cf 100644 --- a/backend/src/main/java/com/worlabel/domain/auth/controller/AuthController.java +++ b/backend/src/main/java/com/worlabel/domain/auth/controller/AuthController.java @@ -44,7 +44,7 @@ public class AuthController { @Operation(summary = "JWT 토큰 재발급", description = "Refresh Token을 확인하여 JWT 토큰 재발급") @SwaggerApiSuccess(description = "Return Access Token") - @SwaggerApiError({ErrorCode.INVALID_TOKEN, ErrorCode.USER_ALREADY_SIGN_OUT, ErrorCode.REFRESH_TOKEN_EXPIRED, ErrorCode.INVALID_REFRESH_TOKEN}) + @SwaggerApiError({ErrorCode.INVALID_TOKEN, ErrorCode.INVALID_REFRESH_TOKEN}) @PostMapping("/reissue") public AccessTokenResponse reissue(HttpServletRequest request, HttpServletResponse response) { log.debug("reissue request"); @@ -66,7 +66,7 @@ public class AuthController { @Operation(summary = "로그인 중인 사용자 정보를 반환", description = "현재 로그인중인 사용자의 정보를 반환합니다.") @SwaggerApiSuccess(description = "Return Member Info") - @SwaggerApiError({ErrorCode.INVALID_TOKEN, ErrorCode.USER_ALREADY_SIGN_OUT, ErrorCode.REFRESH_TOKEN_EXPIRED, ErrorCode.INVALID_REFRESH_TOKEN, ErrorCode.USER_NOT_FOUND}) + @SwaggerApiError({ErrorCode.INVALID_TOKEN, ErrorCode.INVALID_REFRESH_TOKEN, ErrorCode.USER_NOT_FOUND}) @GetMapping("/profile") public MemberResponse getMemberInfo(@CurrentUser Integer currentMember){ return memberService.getMemberId(currentMember); diff --git a/backend/src/main/java/com/worlabel/domain/auth/service/AuthService.java b/backend/src/main/java/com/worlabel/domain/auth/service/AuthService.java index a2dad4c..3b903f6 100644 --- a/backend/src/main/java/com/worlabel/domain/auth/service/AuthService.java +++ b/backend/src/main/java/com/worlabel/domain/auth/service/AuthService.java @@ -26,7 +26,7 @@ public class AuthService { int id = jwtTokenService.parseId(refreshToken); String redisRefreshToken = authCacheRepository.find(id); if(!refreshToken.equals(redisRefreshToken)){ - throw new CustomException(ErrorCode.USER_ALREADY_SIGN_OUT); + throw new CustomException(ErrorCode.INVALID_REFRESH_TOKEN); } return jwtTokenService.generateTokenByRefreshToken(refreshToken); } diff --git a/backend/src/main/java/com/worlabel/domain/auth/service/JwtTokenService.java b/backend/src/main/java/com/worlabel/domain/auth/service/JwtTokenService.java index 8a6e2ec..cd21303 100644 --- a/backend/src/main/java/com/worlabel/domain/auth/service/JwtTokenService.java +++ b/backend/src/main/java/com/worlabel/domain/auth/service/JwtTokenService.java @@ -53,10 +53,7 @@ public class JwtTokenService { } public JwtToken generateTokenByRefreshToken(String refreshToken) throws Exception { - if (isTokenExpired(refreshToken)) { - throw new CustomException(ErrorCode.REFRESH_TOKEN_EXPIRED); - } - if (!isRefreshToken(refreshToken)) { + if (isTokenExpired(refreshToken) | !isRefreshToken(refreshToken)) { throw new CustomException(ErrorCode.INVALID_REFRESH_TOKEN); } diff --git a/backend/src/main/java/com/worlabel/domain/comment/controller/CommentController.java b/backend/src/main/java/com/worlabel/domain/comment/controller/CommentController.java index 4f743d5..8020366 100644 --- a/backend/src/main/java/com/worlabel/domain/comment/controller/CommentController.java +++ b/backend/src/main/java/com/worlabel/domain/comment/controller/CommentController.java @@ -38,7 +38,7 @@ public class CommentController { @GetMapping("/{comment_id}") @SwaggerApiSuccess(description = "댓글을 성공적으로 조회합니다.") @Operation(summary = "댓글 조회", description = "댓글을 조회합니다.") - @SwaggerApiError({ErrorCode.COMMENT_NOT_FOUND, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR}) + @SwaggerApiError({ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR}) public CommentResponse getCommentById( @CurrentUser final Integer memberId, @PathVariable("project_id") final Integer projectId, diff --git a/backend/src/main/java/com/worlabel/domain/comment/service/CommentService.java b/backend/src/main/java/com/worlabel/domain/comment/service/CommentService.java index 8364fb2..d689972 100644 --- a/backend/src/main/java/com/worlabel/domain/comment/service/CommentService.java +++ b/backend/src/main/java/com/worlabel/domain/comment/service/CommentService.java @@ -79,7 +79,7 @@ public class CommentService { Folder folder = image.getFolder(); // 이미지가 속한 폴더를 가져옴 if (!folderRepository.existsByIdAndProjectId(folder.getId(), projectId)) { - throw new CustomException(ErrorCode.IMAGE_NOT_FOUND); + throw new CustomException(ErrorCode.DATA_NOT_FOUND); } } @@ -93,7 +93,7 @@ public class CommentService { Folder folder = image.getFolder(); // 코멘트가 속한 이미지의 폴더를 가져옴 if (!folderRepository.existsByIdAndProjectId(folder.getId(), projectId)) { - throw new CustomException(ErrorCode.COMMENT_NOT_FOUND); + throw new CustomException(ErrorCode.DATA_NOT_FOUND); } } @@ -102,18 +102,18 @@ public class CommentService { */ private void checkAuthorized(final Integer memberId, final Integer projectId) { if (!participantRepository.existsByMemberIdAndProjectId(memberId, projectId)) { - throw new CustomException(ErrorCode.UNAUTHORIZED); + throw new CustomException(ErrorCode.PARTICIPANT_UNAUTHORIZED); } } private Comment getComment(final Integer commentId) { return commentRepository.findById(commentId) - .orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND)); + .orElseThrow(() -> new CustomException(ErrorCode.DATA_NOT_FOUND)); } private Comment getCommentWithMemberId(final Integer commentId, final Integer memberId) { return commentRepository.findByIdAndMemberId(commentId, memberId) - .orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND)); + .orElseThrow(() -> new CustomException(ErrorCode.DATA_NOT_FOUND)); } private Member getMember(final Integer memberId) { @@ -123,6 +123,6 @@ public class CommentService { private Image getImage(final Long imageId) { return imageRepository.findById(imageId) - .orElseThrow(() -> new CustomException(ErrorCode.IMAGE_NOT_FOUND)); + .orElseThrow(() -> new CustomException(ErrorCode.DATA_NOT_FOUND)); } } diff --git a/backend/src/main/java/com/worlabel/domain/folder/service/FolderService.java b/backend/src/main/java/com/worlabel/domain/folder/service/FolderService.java index eed0a04..d7f0703 100644 --- a/backend/src/main/java/com/worlabel/domain/folder/service/FolderService.java +++ b/backend/src/main/java/com/worlabel/domain/folder/service/FolderService.java @@ -4,8 +4,6 @@ import com.worlabel.domain.folder.entity.Folder; import com.worlabel.domain.folder.repository.FolderRepository; import com.worlabel.domain.folder.entity.dto.FolderRequest; import com.worlabel.domain.folder.entity.dto.FolderResponse; -import com.worlabel.domain.participant.entity.PrivilegeType; -import com.worlabel.domain.participant.repository.ParticipantRepository; import com.worlabel.domain.participant.service.ParticipantService; import com.worlabel.domain.project.entity.Project; import com.worlabel.domain.project.repository.ProjectRepository; @@ -104,6 +102,6 @@ public class FolderService { private Folder getFolder(final Integer folderId, final Integer projectId) { return folderRepository.findAllByProjectIdAndId(projectId, folderId) - .orElseThrow(() -> new CustomException(ErrorCode.FOLDER_NOT_FOUND)); + .orElseThrow(() -> new CustomException(ErrorCode.DATA_NOT_FOUND)); } } diff --git a/backend/src/main/java/com/worlabel/domain/image/controller/ImageController.java b/backend/src/main/java/com/worlabel/domain/image/controller/ImageController.java index 82c0671..f5093ae 100644 --- a/backend/src/main/java/com/worlabel/domain/image/controller/ImageController.java +++ b/backend/src/main/java/com/worlabel/domain/image/controller/ImageController.java @@ -54,7 +54,7 @@ public class ImageController { @PutMapping("/{image_id}") @SwaggerApiSuccess(description = "이미지 폴더 이동.") @Operation(summary = "이미지 폴더 이동", description = "이미지가 위치한 폴더를 변경합니다.") - @SwaggerApiError({ErrorCode.BAD_REQUEST, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR, ErrorCode.PARTICIPANT_EDITOR_UNAUTHORIZED, ErrorCode.FOLDER_NOT_FOUND, ErrorCode.IMAGE_NOT_FOUND}) + @SwaggerApiError({ErrorCode.BAD_REQUEST, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR, ErrorCode.PARTICIPANT_EDITOR_UNAUTHORIZED}) public void moveFolderImage( @CurrentUser final Integer memberId, @PathVariable("folder_id") final Integer folderId, @@ -68,7 +68,7 @@ public class ImageController { @DeleteMapping("/{image_id}") @SwaggerApiSuccess(description = "이미지 삭제.") @Operation(summary = "이미지 삭제", description = "폴더에서 해당 이미지를 제거합니다.") - @SwaggerApiError({ErrorCode.BAD_REQUEST, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR, ErrorCode.PARTICIPANT_EDITOR_UNAUTHORIZED, ErrorCode.FOLDER_NOT_FOUND, ErrorCode.IMAGE_NOT_FOUND}) + @SwaggerApiError({ErrorCode.BAD_REQUEST, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR, ErrorCode.PARTICIPANT_EDITOR_UNAUTHORIZED}) public void deleteImage( @CurrentUser final Integer memberId, @PathVariable("folder_id") final Integer folderId, @@ -81,7 +81,7 @@ public class ImageController { @PutMapping("/{image_id}/status") @SwaggerApiSuccess(description = "이미지 상태 변경.") @Operation(summary = "이미지 상태 변경", description = "특정 이미지의 상태를 변경합니다.") - @SwaggerApiError({ErrorCode.BAD_REQUEST, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR, ErrorCode.PARTICIPANT_EDITOR_UNAUTHORIZED, ErrorCode.FOLDER_NOT_FOUND, ErrorCode.IMAGE_NOT_FOUND}) + @SwaggerApiError({ErrorCode.BAD_REQUEST, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR, ErrorCode.PARTICIPANT_EDITOR_UNAUTHORIZED}) public ImageResponse changeImageStatus( @CurrentUser final Integer memberId, @PathVariable("folder_id") final Integer folderId, diff --git a/backend/src/main/java/com/worlabel/domain/image/service/ImageService.java b/backend/src/main/java/com/worlabel/domain/image/service/ImageService.java index 68cda13..801c5c0 100644 --- a/backend/src/main/java/com/worlabel/domain/image/service/ImageService.java +++ b/backend/src/main/java/com/worlabel/domain/image/service/ImageService.java @@ -3,7 +3,6 @@ package com.worlabel.domain.image.service; import com.worlabel.domain.folder.entity.Folder; import com.worlabel.domain.folder.repository.FolderRepository; import com.worlabel.domain.image.entity.Image; -import com.worlabel.domain.image.entity.LabelStatus; import com.worlabel.domain.image.entity.dto.DetailImageResponse; import com.worlabel.domain.image.entity.dto.ImageLabelRequest; import com.worlabel.domain.image.entity.dto.ImageResponse; @@ -108,7 +107,7 @@ public class ImageService { private void save(final long imageId, final String data) { Image image = imageRepository.findById(imageId) - .orElseThrow(() -> new CustomException(ErrorCode.IMAGE_NOT_FOUND)); + .orElseThrow(() -> new CustomException(ErrorCode.DATA_NOT_FOUND)); String dataPath = image.getDataPath(); s3UploadService.uploadJson(data, dataPath); @@ -122,12 +121,12 @@ public class ImageService { // 폴더 가져오기 private Folder getFolder(final Integer folderId) { return folderRepository.findById(folderId) - .orElseThrow(() -> new CustomException(ErrorCode.FOLDER_NOT_FOUND)); + .orElseThrow(() -> new CustomException(ErrorCode.DATA_NOT_FOUND)); } // 이미지 가져오면서 프로젝트 소속 여부를 확인 private Image getImageByIdAndFolderIdAndFolderProjectId(final Integer folderId, final Long imageId, final Integer projectId) { return imageRepository.findByIdAndFolderIdAndFolderProjectId(imageId, folderId, projectId) - .orElseThrow(() -> new CustomException(ErrorCode.IMAGE_NOT_FOUND)); + .orElseThrow(() -> new CustomException(ErrorCode.DATA_NOT_FOUND)); } } diff --git a/backend/src/main/java/com/worlabel/domain/review/service/ReviewService.java b/backend/src/main/java/com/worlabel/domain/review/service/ReviewService.java index 7125dbf..d814a15 100644 --- a/backend/src/main/java/com/worlabel/domain/review/service/ReviewService.java +++ b/backend/src/main/java/com/worlabel/domain/review/service/ReviewService.java @@ -22,8 +22,6 @@ import com.worlabel.global.annotation.CheckPrivilege; import com.worlabel.global.exception.CustomException; import com.worlabel.global.exception.ErrorCode; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -55,7 +53,7 @@ public class ReviewService { List images = imageRepository.findAllById(reviewRequest.getImageIds()); if (images.size() != reviewRequest.getImageIds().size()) { - throw new CustomException(ErrorCode.IMAGE_NOT_FOUND); + throw new CustomException(ErrorCode.DATA_NOT_FOUND); } // 리뷰 이미지 객체 생성 및 배치 저장 @@ -144,11 +142,11 @@ public class ReviewService { private Review getReview(final Integer reviewId) { return reviewRepository.findById(reviewId) - .orElseThrow(() -> new CustomException(ErrorCode.REVIEW_NOT_FOUND)); + .orElseThrow(() -> new CustomException(ErrorCode.DATA_NOT_FOUND)); } private Review getReviewWithMemberId(final Integer reviewId, final Integer memberId) { return reviewRepository.findByIdAndMemberId(reviewId, memberId) - .orElseThrow(() -> new CustomException(ErrorCode.REVIEW_NOT_FOUND)); + .orElseThrow(() -> new CustomException(ErrorCode.DATA_NOT_FOUND)); } } diff --git a/backend/src/main/java/com/worlabel/global/exception/ErrorCode.java b/backend/src/main/java/com/worlabel/global/exception/ErrorCode.java index 6970093..43d8cad 100644 --- a/backend/src/main/java/com/worlabel/global/exception/ErrorCode.java +++ b/backend/src/main/java/com/worlabel/global/exception/ErrorCode.java @@ -9,25 +9,18 @@ import org.springframework.http.HttpStatus; public enum ErrorCode { // Common - 1000 SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, 9999, "서버 에러입니다. 관리자에게 문의해주세요."), - INVALID_INPUT_VALUE(HttpStatus.BAD_REQUEST, 1000, "올바르지 않은 입력 값입니다. 다시 한번 확인해주세요."), - EMPTY_FILE(HttpStatus.BAD_REQUEST, 1001, "빈 파일입니다."), - BAD_REQUEST(HttpStatus.BAD_REQUEST, 1002, "잘못된 요청입니다. 요청을 확인해주세요."), - EMPTY_REQUEST_PARAMETER(HttpStatus.BAD_REQUEST, 1003, "필수 요청 파라미터가 입력되지 않았습니다."), - INVALID_URL(HttpStatus.BAD_REQUEST, 1004, "제공하지 않는 주소입니다. 확인해주세요"), - FAIL_TO_CREATE_FILE(HttpStatus.BAD_REQUEST, 1005, "파일 업로드에 실패하였습니다. 다시 한번 확인해주세요"), - FAIL_TO_DELETE_FILE(HttpStatus.BAD_REQUEST, 1006, "파일 삭제에 실패하였습니다. 다시 한번 확인해주세요"), - INVALID_FILE_PATH(HttpStatus.BAD_REQUEST, 1007, "파일 경로가 잘못되었습니다. 다시 한번 확인해주세요"), + BAD_REQUEST(HttpStatus.BAD_REQUEST, 1000, "잘못된 요청입니다. 요청을 확인해주세요."), + EMPTY_REQUEST_PARAMETER(HttpStatus.BAD_REQUEST, 1001, "필수 요청 파라미터가 입력되지 않았습니다."), + FAIL_TO_CREATE_FILE(HttpStatus.BAD_REQUEST, 1002, "파일 업로드에 실패하였습니다. 다시 한번 확인해주세요"), + FAIL_TO_DELETE_FILE(HttpStatus.BAD_REQUEST, 1003, "파일 삭제에 실패하였습니다. 다시 한번 확인해주세요"), + INVALID_FILE_PATH(HttpStatus.BAD_REQUEST, 1004, "파일 경로가 잘못되었습니다. 다시 한번 확인해주세요"), + DATA_NOT_FOUND(HttpStatus.NOT_FOUND, 1005, "해당 데이터를 찾을 수 없습니다."), // Auth & Member - 2000 USER_NOT_FOUND(HttpStatus.NOT_FOUND, 2000, "해당 ID의 사용자를 찾을 수 없습니다."), - ACCESS_TOKEN_EXPIRED(HttpStatus.UNAUTHORIZED, 2001, "만료된 액세스 토큰입니다."), - REFRESH_TOKEN_MISSING(HttpStatus.BAD_REQUEST, 2002, "리프레시 토큰이 누락되었습니다."), - REFRESH_TOKEN_EXPIRED(HttpStatus.BAD_REQUEST, 2003, "리프레시 토큰이 만료되었습니다."), - INVALID_REFRESH_TOKEN(HttpStatus.BAD_REQUEST, 2004, "유효하지 않은 리프레시 토큰입니다."), - UNAUTHORIZED(HttpStatus.UNAUTHORIZED, 2005, "인증에 실패하였습니다."), - ACCESS_DENIED(HttpStatus.FORBIDDEN, 2006, "접근 권한이 없습니다."), - INVALID_TOKEN(HttpStatus.UNAUTHORIZED, 2007, "올바르지 않는 인증 토큰입니다. 다시 확인 해주세요"), - USER_ALREADY_SIGN_OUT(HttpStatus.UNAUTHORIZED, 2008, "이미 로그아웃한 사용자입니다."), + INVALID_REFRESH_TOKEN(HttpStatus.BAD_REQUEST, 2001, "유효하지 않은 리프레시 토큰입니다."), + ACCESS_DENIED(HttpStatus.FORBIDDEN, 2002, "접근 권한이 없습니다."), + INVALID_TOKEN(HttpStatus.UNAUTHORIZED, 2003, "올바르지 않는 인증 토큰입니다. 다시 확인 해주세요"), // Workspace - 3000 NOT_AUTHOR(HttpStatus.FORBIDDEN, 3001, "작성자가 아닙니다. 이 작업을 수행할 권한이 없습니다."), @@ -46,20 +39,16 @@ public enum ErrorCode { PARTICIPANT_EDITOR_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, 5002, "해당 프로젝트에 수정 권한이 없습니다."), // Folder - 6000, - FOLDER_NOT_FOUND(HttpStatus.NOT_FOUND, 6000, "해당 폴더를 찾을 수 없습니다."), - FOLDER_UNAUTHORIZED(HttpStatus.UNAUTHORIZED, 6001, "해당 폴더에 접근 권한이 없습니다."), // Image - 7000 - IMAGE_NOT_FOUND(HttpStatus.NOT_FOUND, 7000, "해당 이미지를 찾을 수 없습니다."), // AI - 8000 AI_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, 8000, "AI 서버 오류 입니다."), // Comment - 9000, - COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, 9000, "해당 댓글을 찾을 수 없습니다."), - // Comment - 10000, - REVIEW_NOT_FOUND(HttpStatus.NOT_FOUND, 10000, "해당 리뷰를 찾을 수 없습니다."), + // review - 10000, + ; private final HttpStatus status; private final int code; diff --git a/backend/src/main/java/com/worlabel/global/filter/JwtAuthenticationFilter.java b/backend/src/main/java/com/worlabel/global/filter/JwtAuthenticationFilter.java index 7ea0da2..328c5d3 100644 --- a/backend/src/main/java/com/worlabel/global/filter/JwtAuthenticationFilter.java +++ b/backend/src/main/java/com/worlabel/global/filter/JwtAuthenticationFilter.java @@ -50,7 +50,6 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter { } } catch (Exception e) { log.debug("message: {}", e.getMessage()); - SecurityContextHolder.clearContext(); request.setAttribute("error-message", e.getMessage()); } filterChain.doFilter(request, response); diff --git a/backend/src/main/java/com/worlabel/global/service/S3UploadService.java b/backend/src/main/java/com/worlabel/global/service/S3UploadService.java index 571c0bb..e543318 100644 --- a/backend/src/main/java/com/worlabel/global/service/S3UploadService.java +++ b/backend/src/main/java/com/worlabel/global/service/S3UploadService.java @@ -93,7 +93,7 @@ public class S3UploadService { */ public String upload(final MultipartFile image, final String extension, final Integer projectId) { if (image.isEmpty() || Objects.isNull(image.getOriginalFilename())) { - throw new CustomException(ErrorCode.EMPTY_FILE); + throw new CustomException(ErrorCode.BAD_REQUEST); } return url + "/" + uploadImage(image, extension, projectId); }