Skip to content

Commit

Permalink
Fix: 에러 처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
saokiritoni committed Dec 3, 2024
1 parent a35b704 commit 80a8ba7
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,7 +74,7 @@ public List<PortfolioBaseDto> 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);
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/dongguk/osori/global/exception/CustomException.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
46 changes: 46 additions & 0 deletions src/main/java/dongguk/osori/global/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions src/main/java/dongguk/osori/global/exception/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<ErrorResponse> handleCustomException(CustomException ex) {
return ResponseEntity
.status(ex.getErrorCode().getStatus())
.body(ErrorResponse.of(ex.getErrorCode()));
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception ex) {
return ResponseEntity
.status(ErrorCode.INTERNAL_SERVER_ERROR.getStatus())
.body(ErrorResponse.of(ErrorCode.INTERNAL_SERVER_ERROR));
}
}

0 comments on commit 80a8ba7

Please sign in to comment.