Fix small API bug and better status feedback.

This commit is contained in:
gerlofvanek 2025-01-16 12:00:50 +01:00
parent 4d928dc98e
commit b14fba0e1f
2 changed files with 22 additions and 7 deletions

View file

@ -1183,6 +1183,7 @@ async function fetchPricesWithApiKey(baseUrl) {
}); });
if (!response.ok) { if (!response.ok) {
console.log('API key fetch failed with status:', response.status);
return null; return null;
} }
@ -1190,6 +1191,7 @@ async function fetchPricesWithApiKey(baseUrl) {
if (data && !data.Error && Object.keys(data).length > 0) { if (data && !data.Error && Object.keys(data).length > 0) {
return data; return data;
} }
console.log('API key fetch returned invalid data');
return null; return null;
} catch (error) { } catch (error) {
console.warn('Failed to fetch prices with API key:', error); console.warn('Failed to fetch prices with API key:', error);
@ -1245,6 +1247,8 @@ function getEmptyPriceData() {
async function fetchLatestPrices() { async function fetchLatestPrices() {
const PRICES_CACHE_KEY = 'prices_coingecko'; const PRICES_CACHE_KEY = 'prices_coingecko';
const RETRY_DELAY = 5000;
const MAX_RETRIES = 3;
const cachedData = CacheManager.get(PRICES_CACHE_KEY); const cachedData = CacheManager.get(PRICES_CACHE_KEY);
if (cachedData && cachedData.remainingTime > 60000) { if (cachedData && cachedData.remainingTime > 60000) {
@ -1255,18 +1259,29 @@ async function fetchLatestPrices() {
const baseUrl = `${offersConfig.apiEndpoints.coinGecko}/simple/price?ids=bitcoin,bitcoin-cash,dash,dogecoin,decred,litecoin,particl,pivx,monero,zano,wownero,zcoin&vs_currencies=USD,BTC`; const baseUrl = `${offersConfig.apiEndpoints.coinGecko}/simple/price?ids=bitcoin,bitcoin-cash,dash,dogecoin,decred,litecoin,particl,pivx,monero,zano,wownero,zcoin&vs_currencies=USD,BTC`;
let data = await fetchPricesWithApiKey(baseUrl); let retryCount = 0;
let data = null;
while (!data && retryCount < MAX_RETRIES) {
data = await fetchPricesWithApiKey(baseUrl);
if (!data) { if (!data) {
console.log('API key attempt failed, trying without API key...');
data = await fetchPricesWithoutApiKey(baseUrl); data = await fetchPricesWithoutApiKey(baseUrl);
} }
if (!data && retryCount < MAX_RETRIES - 1) {
console.log(`Attempt ${retryCount + 1} failed, waiting ${RETRY_DELAY}ms before retry...`);
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
retryCount++;
}
}
if (data) { if (data) {
console.log('Processing fresh price data...'); console.log('Successfully fetched fresh price data');
latestPrices = data; latestPrices = data;
CacheManager.set(PRICES_CACHE_KEY, data, CACHE_DURATION); CacheManager.set(PRICES_CACHE_KEY, data, CACHE_DURATION);
// Set fallback values
Object.entries(data).forEach(([coin, prices]) => { Object.entries(data).forEach(([coin, prices]) => {
tableRateModule.setFallbackValue(coin, prices.usd); tableRateModule.setFallbackValue(coin, prices.usd);
}); });
@ -1274,7 +1289,7 @@ async function fetchLatestPrices() {
return data; return data;
} }
console.warn('All price fetch attempts failed, using N/A values'); console.warn('All price fetch attempts failed after retries, using N/A values');
const naData = getEmptyPriceData(); const naData = getEmptyPriceData();
latestPrices = naData; latestPrices = naData;
return naData; return naData;

View file

@ -147,7 +147,7 @@ function getWebSocketConfig() {
</button> </button>
<p class="text-red-600 font-semibold text-xl mb-4">Error</p> <p class="text-red-600 font-semibold text-xl mb-4">Error</p>
<p id="error-message" class="text-gray-700 dark:text-gray-300 text-lg mb-6"></p> <p id="error-message" class="text-gray-700 dark:text-gray-300 text-lg mb-6"></p>
<p class="text-sm text-gray-600 dark:text-gray-400">To review or update your Chart API Key(s), navigate to<a href="/settings" class="text-blue-500 hover:underline">Settings & Tools > Settings > General (TAB)</a>. <p class="text-sm text-gray-600 dark:text-gray-400">To review or update your Chart API Key(s), navigate to <a href="/settings" class="text-blue-500 hover:underline">Settings & Tools > Settings > General (TAB)</a>.
</p> </p>
</div> </div>
</div> </div>