Skip to content

Commit

Permalink
#53 [feat] : Market 에러 처리 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbang105 committed Jun 9, 2024
1 parent 24a4a59 commit 42d93af
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.extern.slf4j.Slf4j;
import org.dgu.backend.common.ApiResponse;
import org.dgu.backend.common.code.BaseErrorCode;
import org.dgu.backend.domain.Market;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingRequestHeaderException;
Expand Down Expand Up @@ -68,4 +69,10 @@ public ResponseEntity<ApiResponse<BaseErrorCode>> handleChartException(ChartExce
ChartErrorResult errorResult = e.getChartErrorResult();
return ApiResponse.onFailure(errorResult);
}
// Market
@ExceptionHandler(MarketException.class)
public ResponseEntity<ApiResponse<BaseErrorCode>> handleMarketException(MarketException e) {
MarketErrorResult errorResult = e.getMarketErrorResult();
return ApiResponse.onFailure(errorResult);
}
}
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 MarketErrorResult implements BaseErrorCode {
NOT_FOUND_MARKET(HttpStatus.NOT_FOUND, "404", "존재하지 않는 가상화폐입니다."),
NOT_FOUND_MARKETS(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 MarketException extends RuntimeException {
private final MarketErrorResult marketErrorResult;

@Override
public String getMessage() {
return marketErrorResult.getMessage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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.repository.CandleInfoRepository;
import org.dgu.backend.repository.CandleRepository;
import org.dgu.backend.repository.MarketRepository;
Expand All @@ -16,6 +18,7 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
Expand All @@ -39,6 +42,9 @@ public List<ChartDto.OHLCVResponse> getOHLCVCharts(String koreanName, String can
@Override
public List<ChartDto.ChartOptionResponse> getAllChartOptions() {
List<Market> markets = marketRepository.findAll();
if (markets.isEmpty()) {
throw new MarketException(MarketErrorResult.NOT_FOUND_MARKETS);
}
List<Candle> candles = candleRepository.findAll();

return markets.stream()
Expand All @@ -57,6 +63,9 @@ protected void updateCandleInfo(String koreanName, String candleName) {
@Transactional
protected List<ChartDto.OHLCVResponse> fetchUpdatedCandleInfo(String koreanName, String candleName) {
Market market = marketRepository.findByKoreanName(koreanName);
if (Objects.isNull(market)) {
throw new MarketException(MarketErrorResult.NOT_FOUND_MARKET);
}
Candle candle = candleRepository.findByCandleName(candleName);
LocalDateTime startDate = candleUtil.getStartDateByCandleName(candleName);

Expand Down

0 comments on commit 42d93af

Please sign in to comment.