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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased (develop)

- added: More robust error handling in `SwipeChart` to handle rate limits
- added: New Banxa payment methods

## 4.45.0 (staging)
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate 429 retry logic may drift

Low Severity

Two nearly identical blocks handle 429 retries — one for the HTTP status code (before JSON parsing) and one for the JSON body error code (after parsing). Both perform the same pro-URL fallback check, snooze(2000), and continue. If future changes update one block (e.g., adding a retry counter or exponential backoff), the other could easily be missed, leading to inconsistent retry behavior.

Additional Locations (1)
Fix in Cursor Fix in Web

}
// 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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unbounded retry loop on 429 can spin forever

High Severity

The do...while(true) loop retries indefinitely on HTTP 429 responses with no maxRetries counter or exit condition beyond eventual success. If the API keeps rate-limiting, the loop never exits, setIsFetching(false) in the outer finally is never reached, and the user sees a permanent loading spinner. The PR discussion states that maxRetries (increased from 3 to 5) and exponential backoff were added, but neither is present in the code. The codebase already has a bounded retry pattern in FirstOpenActions.tsx using a for loop with maxRetries.

Additional Locations (1)
Fix in Cursor Fix in Web

} finally {
setIsFetching(false)
}
},
[selectedTimespan, isConnected, fetchAssetId, coingeckoFiat],
'swipeChart'
Expand Down
Loading