From 2e3b0209b50dfbb8ee5842787ab20ce03deacf0a Mon Sep 17 00:00:00 2001 From: sairaj mote Date: Tue, 16 Jan 2024 15:28:49 +0530 Subject: [PATCH] error handling if historic api takes too long to respond --- index.html | 24 +++++++++++++++++------- index.min.html | 30 +++++++++++++++--------------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/index.html b/index.html index d80ba3f..b168173 100644 --- a/index.html +++ b/index.html @@ -1274,9 +1274,19 @@ try { if (!dates.length) return []; if (!Array.isArray(dates)) dates = [dates]; + let timeout const datesToFetch = dates.filter(date => !mappedHistoricPrices.has(date)); + const abortController = new AbortController(); + const { signal } = abortController; if (datesToFetch.length) { - const historicPrices = await fetch(`${historicPriceApis.list[historicPriceApis.active]}/price-history?dates=${dates.join()}`).then(res => res.json()) + timeout = setTimeout(() => { + abortController.abort() + throw new Error(`Historic price data is taking longer than usual to load. Historic prices won't be shown`) + }, 8000) + const historicPrices = await fetch(`${historicPriceApis.list[historicPriceApis.active]}/price-history?dates=${dates.join()}`, { + signal + }).then(res => res.json()) + clearTimeout(timeout) historicPrices.forEach(price => { // map historic price to txs // historic price is per day, so we need to find the price for the day of the tx @@ -1292,11 +1302,11 @@ historicPriceApis.active++ return getHistoricPrice(dates) } else { - selectedCurrency = 'btc' - showCurrentValue = false - localStorage.setItem('btc-wallet-currency', selectedCurrency) - localStorage.setItem('btc-wallet-show-current-value', showCurrentValue) - notify('There was an error fetching historic price data. Please try again later.', 'error') + showCurrentValue = true; + localStorage.setItem('btc-wallet-show-current-value', showCurrentValue); + document.getElementById('show_current_value').checked = showCurrentValue; + notify('Could not fetch historic price data. Showing current value instead', 'error') + return [] } } } @@ -1323,7 +1333,7 @@ // render transactions if (txs.length) { const dates = txs.map(tx => toYDM(tx.time)) - await getHistoricPrice(dates) + await getHistoricPrice(dates); txs.forEach(tx => { const historicPrice = mappedHistoricPrices.get(toYDM(tx.time)) if (historicPrice) { diff --git a/index.min.html b/index.min.html index dd49312..b061a4d 100644 --- a/index.min.html +++ b/index.min.html @@ -1,5 +1,5 @@ - Bitcoin Wallet
Getting Bitcoin Wallet ready
Bitcoin Web Wallet logo
RanchiMall

Bitcoin Wallet

BTC INR USD

Keep your keys safe!

Don't share with anyone. The private key cannot be recovered if lost.

Did you forget your BTC address?

If you have your private key, enter it here and recover your BTC address.