diff --git a/backend/src/main/java/org/dgu/backend/exception/CandleErrorResult.java b/backend/src/main/java/org/dgu/backend/exception/CandleErrorResult.java new file mode 100644 index 0000000..229398e --- /dev/null +++ b/backend/src/main/java/org/dgu/backend/exception/CandleErrorResult.java @@ -0,0 +1,37 @@ +package org.dgu.backend.exception; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.dgu.backend.common.code.BaseErrorCode; +import org.dgu.backend.common.dto.ErrorReasonDto; +import org.springframework.http.HttpStatus; + +@Getter +@RequiredArgsConstructor +public enum CandleErrorResult implements BaseErrorCode { + NOT_FOUND_CANDLE(HttpStatus.NOT_FOUND, "404", "존재하지 않는 캔들입니다."), + NOT_FOUND_CANDLES(HttpStatus.NOT_FOUND, "404", "캔들 목록이 존재하지 않습니다."); + + private final HttpStatus httpStatus; + private final String code; + private final String message; + + @Override + public ErrorReasonDto getReason() { + return ErrorReasonDto.builder() + .isSuccess(false) + .code(code) + .message(message) + .build(); + } + + @Override + public ErrorReasonDto getReasonHttpStatus() { + return ErrorReasonDto.builder() + .isSuccess(false) + .httpStatus(httpStatus) + .code(code) + .message(message) + .build(); + } +} \ No newline at end of file diff --git a/backend/src/main/java/org/dgu/backend/exception/CandleException.java b/backend/src/main/java/org/dgu/backend/exception/CandleException.java new file mode 100644 index 0000000..d58a4df --- /dev/null +++ b/backend/src/main/java/org/dgu/backend/exception/CandleException.java @@ -0,0 +1,15 @@ +package org.dgu.backend.exception; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public class CandleException extends RuntimeException { + private final CandleErrorResult candleErrorResult; + + @Override + public String getMessage() { + return candleErrorResult.getMessage(); + } +} \ No newline at end of file diff --git a/backend/src/main/java/org/dgu/backend/exception/GlobalExceptionHandler.java b/backend/src/main/java/org/dgu/backend/exception/GlobalExceptionHandler.java index bd0b918..9cca2c0 100644 --- a/backend/src/main/java/org/dgu/backend/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/org/dgu/backend/exception/GlobalExceptionHandler.java @@ -75,4 +75,10 @@ public ResponseEntity> handleMarketException(MarketEx MarketErrorResult errorResult = e.getMarketErrorResult(); return ApiResponse.onFailure(errorResult); } + // Candle + @ExceptionHandler(CandleException.class) + public ResponseEntity> handleCandleException(CandleException e) { + CandleErrorResult errorResult = e.getCandleErrorResult(); + return ApiResponse.onFailure(errorResult); + } } \ No newline at end of file diff --git a/backend/src/main/java/org/dgu/backend/service/ChartServiceImpl.java b/backend/src/main/java/org/dgu/backend/service/ChartServiceImpl.java index 7de8bfb..972b802 100644 --- a/backend/src/main/java/org/dgu/backend/service/ChartServiceImpl.java +++ b/backend/src/main/java/org/dgu/backend/service/ChartServiceImpl.java @@ -6,10 +6,7 @@ import org.dgu.backend.domain.CandleInfo; import org.dgu.backend.domain.Market; import org.dgu.backend.dto.ChartDto; -import org.dgu.backend.exception.ChartErrorResult; -import org.dgu.backend.exception.ChartException; -import org.dgu.backend.exception.MarketErrorResult; -import org.dgu.backend.exception.MarketException; +import org.dgu.backend.exception.*; import org.dgu.backend.repository.CandleInfoRepository; import org.dgu.backend.repository.CandleRepository; import org.dgu.backend.repository.MarketRepository; @@ -46,6 +43,9 @@ public List getAllChartOptions() { throw new MarketException(MarketErrorResult.NOT_FOUND_MARKETS); } List candles = candleRepository.findAll(); + if (candles.isEmpty()) { + throw new CandleException(CandleErrorResult.NOT_FOUND_CANDLES); + } return markets.stream() .flatMap(market -> candles.stream() @@ -67,6 +67,9 @@ protected List fetchUpdatedCandleInfo(String koreanName, throw new MarketException(MarketErrorResult.NOT_FOUND_MARKET); } Candle candle = candleRepository.findByCandleName(candleName); + if (Objects.isNull(candle)) { + throw new CandleException(CandleErrorResult.NOT_FOUND_CANDLE); + } LocalDateTime startDate = candleUtil.getStartDateByCandleName(candleName); List candleInfos = candleInfoRepository.findByMarketAndCandleAndDateTimeAfter(market, candle, startDate);