Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased (develop)

- added: More robust error handling in `SwipeChart` to handle rate limits

## 4.44.0 (staging)

- added: MAYAChain (CACAO) wallet support
Expand Down
1 change: 0 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ export default [
'src/components/cards/VisaCardCard.tsx',
'src/components/cards/WalletRestoreCard.tsx',
'src/components/cards/WarningCard.tsx',
'src/components/charts/SwipeChart.tsx',

'src/components/common/AnimatedNumber.tsx',
'src/components/common/BlurBackground.tsx',
Expand Down
107 changes: 67 additions & 40 deletions src/components/charts/SwipeChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,49 +286,76 @@ export const SwipeChart: React.FC<Props> = props => {
)
// Start with the free base URL
let fetchUrl = `${COINGECKO_URL}${fetchPath}`
do {
try {
// Construct the dataset query
const response = await fetch(fetchUrl)
const result = await response.json()
const apiError = asMaybe(asCoinGeckoError)(result)
if (apiError != null) {
if (apiError.status.error_code === 429) {
// Rate limit error, use our API key as a fallback
if (
!fetchUrl.includes('x_cg_pro_api_key') &&
ENV.COINGECKO_API_KEY !== ''
) {
fetchUrl = `${COINGECKO_URL_PRO}${fetchPath}&x_cg_pro_api_key=${ENV.COINGECKO_API_KEY}`
try {
do {
try {
const response = await fetch(fetchUrl)

// Handle non-OK responses before parsing JSON
if (!response.ok) {
if (response.status === 429) {
if (
!fetchUrl.includes('x_cg_pro_api_key') &&
ENV.COINGECKO_API_KEY !== ''
) {
fetchUrl = `${COINGECKO_URL_PRO}${fetchPath}&x_cg_pro_api_key=${ENV.COINGECKO_API_KEY}`
}
// Wait 2 seconds before retrying. It typically takes 1 minute
// before rate limiting is relieved, so even 2 seconds is hasty.
await snooze(2000)
continue
}
// Wait 2 second before retrying. It typically takes 1 minute
// before rate limiting is relieved, so even 2 seconds is hasty.
await snooze(2000)
continue
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
}
throw new Error(
`Failed to fetch market data: ${apiError.status.error_code} ${apiError.status.error_message}`
)
}

const marketChartRange = asCoinGeckoMarketChartRange(result)
const rawChartData = marketChartRange.prices.map(pair => ({
x: new Date(pair[0]),
y: pair[1]
}))
const reduced = reduceChartData(rawChartData, selectedTimespan)

setChartData(reduced)
cachedTimespanChartData.set(selectedTimespan, reduced)
setCachedChartData(cachedTimespanChartData)
} catch (e: unknown) {
console.error(JSON.stringify(e))
setErrorMessage(lstrings.error_data_unavailable)
} finally {
setIsFetching(false)
}
break
} while (true)
// Parse JSON safely - use text() first to catch parse errors locally
const text = await response.text()
let result: unknown
try {
result = JSON.parse(text)
} catch {
throw new Error(`Invalid JSON response: ${text.slice(0, 100)}...`)
}

const apiError = asMaybe(asCoinGeckoError)(result)
if (apiError != null) {
if (apiError.status.error_code === 429) {
if (
!fetchUrl.includes('x_cg_pro_api_key') &&
ENV.COINGECKO_API_KEY !== ''
) {
fetchUrl = `${COINGECKO_URL_PRO}${fetchPath}&x_cg_pro_api_key=${ENV.COINGECKO_API_KEY}`
}
// Wait 2 seconds before retrying. It typically takes 1 minute
// before rate limiting is relieved, so even 2 seconds is hasty.
await snooze(2000)
continue
}
throw new Error(
`Failed to fetch market data: ${apiError.status.error_code} ${apiError.status.error_message}`
)
}

const marketChartRange = asCoinGeckoMarketChartRange(result)
const rawChartData = marketChartRange.prices.map(pair => ({
x: new Date(pair[0]),
y: pair[1]
}))
const reduced = reduceChartData(rawChartData, selectedTimespan)

setChartData(reduced)
cachedTimespanChartData.set(selectedTimespan, reduced)
setCachedChartData(cachedTimespanChartData)
} catch (e: unknown) {
const message = e instanceof Error ? e.message : String(e)
console.error('SwipeChart fetch error:', message)
setErrorMessage(lstrings.error_data_unavailable)
}
break
} while (true)
} finally {
setIsFetching(false)
}
},
[selectedTimespan, isConnected, fetchAssetId, coingeckoFiat],
'swipeChart'
Expand Down
Loading