Skip to content

Commit

Permalink
Merge pull request #44 from CSID-DGU/develop
Browse files Browse the repository at this point in the history
โ™ป๏ธ [Refactor] ํฌํŠธํด๋ฆฌ์˜ค ์ƒ์„ธ์กฐํšŒ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ์ถ”๊ฐ€
  • Loading branch information
saokiritoni authored Dec 3, 2024
2 parents 8959d1b + 80a8ba7 commit 12acf5e
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 12acf5e

Please sign in to comment.