From 80a8ba7553036a2c8fe0bdeefe7b62ae93db9209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?toni=20=28=EC=9D=B4=EC=86=8C=EC=9D=80=29?= <144209738+saokiritoni@users.noreply.github.com> Date: Tue, 3 Dec 2024 15:58:39 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20=EC=97=90=EB=9F=AC=20=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../portfolio/service/PortfolioService.java | 4 +- .../global/exception/CustomException.java | 13 ++++++ .../osori/global/exception/ErrorCode.java | 46 +++++++++++++++++++ .../osori/global/exception/ErrorResponse.java | 18 ++++++++ .../exception/GlobalExceptionHandler.java | 23 ++++++++++ 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/main/java/dongguk/osori/global/exception/CustomException.java create mode 100644 src/main/java/dongguk/osori/global/exception/ErrorCode.java create mode 100644 src/main/java/dongguk/osori/global/exception/ErrorResponse.java create mode 100644 src/main/java/dongguk/osori/global/exception/GlobalExceptionHandler.java diff --git a/src/main/java/dongguk/osori/domain/portfolio/service/PortfolioService.java b/src/main/java/dongguk/osori/domain/portfolio/service/PortfolioService.java index f6320fa..ce9a489 100644 --- a/src/main/java/dongguk/osori/domain/portfolio/service/PortfolioService.java +++ b/src/main/java/dongguk/osori/domain/portfolio/service/PortfolioService.java @@ -7,6 +7,8 @@ import dongguk.osori.domain.quest.service.QuestService; import dongguk.osori.domain.user.entity.User; import dongguk.osori.domain.user.repository.UserRepository; +import dongguk.osori.global.exception.CustomException; +import dongguk.osori.global.exception.ErrorCode; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -72,7 +74,7 @@ public List getPortfolioList(Long userId) { @Transactional(readOnly = true) public PortfolioDetailDto getPortfolioDetail(Long userId, Long portfolioId) { Portfolio portfolio = portfolioRepository.findPortfolioWithDetails(portfolioId, userId) - .orElseThrow(() -> new IllegalArgumentException("Portfolio not found or access denied for ID: " + portfolioId)); + .orElseThrow(() -> new CustomException(ErrorCode.PORTFOLIO_NOT_FOUND)); return mapToDetailDto(portfolio); } diff --git a/src/main/java/dongguk/osori/global/exception/CustomException.java b/src/main/java/dongguk/osori/global/exception/CustomException.java new file mode 100644 index 0000000..ddb40f8 --- /dev/null +++ b/src/main/java/dongguk/osori/global/exception/CustomException.java @@ -0,0 +1,13 @@ +package dongguk.osori.global.exception; + +import lombok.Getter; + +@Getter +public class CustomException extends RuntimeException { + private final ErrorCode errorCode; + + public CustomException(ErrorCode errorCode) { + super(errorCode.getMessage()); + this.errorCode = errorCode; + } +} diff --git a/src/main/java/dongguk/osori/global/exception/ErrorCode.java b/src/main/java/dongguk/osori/global/exception/ErrorCode.java new file mode 100644 index 0000000..3a3021a --- /dev/null +++ b/src/main/java/dongguk/osori/global/exception/ErrorCode.java @@ -0,0 +1,46 @@ +package dongguk.osori.global.exception; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum ErrorCode { + // global + INVALID_INPUT_VALUE(400, "잘못된 입력 값입니다."), + INTERNAL_SERVER_ERROR(500, "서버 오류가 발생했습니다."), + UNAUTHORIZED_ACCESS(401, "권한이 없습니다."), + INVALID_REQUEST(400, "잘못된 요청입니다."), + FORBIDDEN_ACCESS(403, "접근 권한이 없습니다."), + SESSION_EXPIRED(401, "세션이 만료되었습니다."), + INVALID_REQUEST_BODY(400, "요청 본문이 잘못되었습니다."), + MISSING_REQUIRED_FIELD(400, "필수 필드가 누락되었습니다."), + INVALID_DATA_FORMAT(400, "유효하지 않은 데이터 형식입니다."), + INVALID_FIELD_VALUE(422, "요청 본문의 데이터가 유효하지 않습니다."), + FIELD_VALIDATION_FAILED(422, "필드 값 검증에 실패했습니다."), + UNSUPPORTED_MEDIA_TYPE(415, "지원하지 않는 Content-Type입니다."), + + + // user + UNAUTHORIZED(403, "인증되지 않은 사용자입니다."), + USER_NOT_FOUND(404, "사용자를 찾을 수 없습니다."), + SESSION_USER_NOT_FOUND(401, "세션에 userId가 없습니다."), + COUPLE_ONLY_ACCESS(403, "커플인 경우에만 접근할 수 있습니다."), + + // policy + POLICY_NOT_FOUND(404, "해당 정책을 찾을 수 없습니다."), + POLICY_ALREADY_EXISTS(409, "이미 존재하는 정책입니다."), + + + // comment + COMMENT_NOT_FOUND(404, "댓글을 찾을 수 없습니다."), + COMMENT_DELETE_FORBIDDEN(403, "댓글 삭제 권한이 없습니다."), + + // portfolio + PORTFOLIO_NOT_FOUND(404, "포트폴리오를 찾을 수 없습니다."), + PORTFOLIO_ACCESS_FORBIDDEN(403, "해당 포트폴리오에 접근할 권한이 없습니다."); + + + private final int status; + private final String message; +} diff --git a/src/main/java/dongguk/osori/global/exception/ErrorResponse.java b/src/main/java/dongguk/osori/global/exception/ErrorResponse.java new file mode 100644 index 0000000..1c4da29 --- /dev/null +++ b/src/main/java/dongguk/osori/global/exception/ErrorResponse.java @@ -0,0 +1,18 @@ +package dongguk.osori.global.exception; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class ErrorResponse { + private final int status; + private final String message; + + public static ErrorResponse of(ErrorCode errorCode) { + return ErrorResponse.builder() + .status(errorCode.getStatus()) + .message(errorCode.getMessage()) + .build(); + } +} diff --git a/src/main/java/dongguk/osori/global/exception/GlobalExceptionHandler.java b/src/main/java/dongguk/osori/global/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..4f7bfd2 --- /dev/null +++ b/src/main/java/dongguk/osori/global/exception/GlobalExceptionHandler.java @@ -0,0 +1,23 @@ +package dongguk.osori.global.exception; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(CustomException.class) + public ResponseEntity handleCustomException(CustomException ex) { + return ResponseEntity + .status(ex.getErrorCode().getStatus()) + .body(ErrorResponse.of(ex.getErrorCode())); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleException(Exception ex) { + return ResponseEntity + .status(ErrorCode.INTERNAL_SERVER_ERROR.getStatus()) + .body(ErrorResponse.of(ErrorCode.INTERNAL_SERVER_ERROR)); + } +}