import { scope, exponentialBackoff } from 'go-go-scope';
async function retryOnSpecificErrors() {
await using s = scope();
const [err, data] = await s.task(
async () => {
const response = await fetch('/api/data');
if (response.status === 429) {
// Rate limited - retry
throw new Error('RATE_LIMIT');
}
if (response.status >= 500) {
// Server error - retry
throw new Error('SERVER_ERROR');
}
if (!response.ok) {
// Client error - don't retry
throw new Error('CLIENT_ERROR');
}
return response.json();
},
{
retry: {
maxRetries: 3,
delay: exponentialBackoff(),
condition: (error) => {
// Only retry on rate limit or server errors
return (
error instanceof Error &&
(error.message === 'RATE_LIMIT' || error.message === 'SERVER_ERROR')
);
},
},
}
);
return data;
}