Skip to content

Commit

Permalink
#53 [feat] : Candle 에러 처리 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbang105 committed Jun 9, 2024
1 parent 42d93af commit ecbb1cd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,10 @@ public ResponseEntity<ApiResponse<BaseErrorCode>> handleMarketException(MarketEx
MarketErrorResult errorResult = e.getMarketErrorResult();
return ApiResponse.onFailure(errorResult);
}
// Candle
@ExceptionHandler(CandleException.class)
public ResponseEntity<ApiResponse<BaseErrorCode>> handleCandleException(CandleException e) {
CandleErrorResult errorResult = e.getCandleErrorResult();
return ApiResponse.onFailure(errorResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,6 +43,9 @@ public List<ChartDto.ChartOptionResponse> getAllChartOptions() {
throw new MarketException(MarketErrorResult.NOT_FOUND_MARKETS);
}
List<Candle> candles = candleRepository.findAll();
if (candles.isEmpty()) {
throw new CandleException(CandleErrorResult.NOT_FOUND_CANDLES);
}

return markets.stream()
.flatMap(market -> candles.stream()
Expand All @@ -67,6 +67,9 @@ protected List<ChartDto.OHLCVResponse> 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<CandleInfo> candleInfos = candleInfoRepository.findByMarketAndCandleAndDateTimeAfter(market, candle, startDate);
Expand Down

0 comments on commit ecbb1cd

Please sign in to comment.