fix(fetcher): only retry 503 and 504

This commit is contained in:
WebStreamr 2025-09-16 14:49:13 +00:00
parent c10ca29fda
commit d90cd8f984
No known key found for this signature in database
2 changed files with 4 additions and 4 deletions

View file

@ -216,14 +216,14 @@ describe('fetch', () => {
test('passes through other error as HttpError after retrying 3 times', async () => {
const mockPool = mockAgent.get('https://some-error-url.test');
mockPool.intercept({ path: '/' }).reply(500, undefined, { headers: { 'x-foo': 'bar' } }).times(4);
mockPool.intercept({ path: '/' }).reply(503, undefined, { headers: { 'x-foo': 'bar' } }).times(4);
try {
await fetcher.text(ctx, new URL('https://some-error-url.test/'));
fail();
} catch (error) {
expect(error).toBeInstanceOf(HttpError);
expect(error).toMatchObject({ status: 500, headers: { 'x-foo': 'bar' } });
expect(error).toMatchObject({ status: 503, headers: { 'x-foo': 'bar' } });
}
});

View file

@ -316,8 +316,8 @@ export class Fetcher {
}
}
if (response.status >= 500 && tryCount < 3) {
this.logger.warn(`Retrying fetch ${init?.method ?? 'GET'} ${url} because of error`, ctx);
if ([503, 504].includes(response.status) && tryCount < 3) {
this.logger.warn(`Retrying fetch ${init?.method ?? 'GET'} ${url} because of a temporary server error`, ctx);
await this.sleep(333);
return await this.fetchWithTimeout(ctx, url, init, ++tryCount);