From 45e5ecf4181c99dad349b82336b8037661bf4a21 Mon Sep 17 00:00:00 2001 From: Billy Leung <82518709+bleunguts@users.noreply.github.com> Date: Sat, 27 Jan 2024 15:44:32 +0000 Subject: [PATCH] Handle too many market data requests gracefully --- Web/src/TradingStrategyStore.ts | 37 +++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/Web/src/TradingStrategyStore.ts b/Web/src/TradingStrategyStore.ts index 3903d9e..8c36817 100644 --- a/Web/src/TradingStrategyStore.ts +++ b/Web/src/TradingStrategyStore.ts @@ -33,24 +33,43 @@ export class TradingStrategyStore ); } + error = () => { + this.symbol = `ERROR LOADING SYMBOL..`; + this.data = FakeStrategyPlaceholder; + }; + loadChartData(symbol: string) { try { this.isLoading = true; runInAction(() => { this.transport .fetchLongShortStrategy(symbol) - .then((res) => this.data = ((res as AxiosResponse).data as ChartData[])) + .then((res) => { + const resp = (res as AxiosResponse); + switch(resp.status) { + case 204: { + const cache = cachedData(symbol); + if(cache != undefined){ + this.data = cache as ChartData[]; + } else { + this.error(); + } + break; + } + case 500: { + this.error(); + break; + } + default: { + this.data = (resp.data as ChartData[]); + break; + } + } + }) .catch((e) => { console.log(`Error occurred whilst loading new symbol... ${e}`); console.log(e); - const cache = cachedData(symbol); - if (cache != undefined){ - this.data = cache as ChartData[]; - - } else { - this.symbol = `ERROR LOADING SYMBOL..`; - this.data = FakeStrategyPlaceholder; - } + this.error(); }); console.log(`Symbol: ${symbol} loaded, data length: ${this.data.length}`);