Skip to content

Commit c62b158

Browse files
Ignore window.fetch network error (#38)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 76cf526 commit c62b158

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ declare const pRetry: {
5252
/**
5353
Returns a `Promise` that is fulfilled when calling `input` returns a fulfilled promise. If calling `input` returns a rejected promise, `input` is called again until the max retries are reached, it then rejects with the last rejection reason.
5454
55-
It doesn't retry on `TypeError` as that's a user error.
55+
It doesn't retry on `TypeError` as that's a user error. The only exclusion to this logic is when `TypeError` is thrown by `fetch`'s API with the message 'Failed to fetch', [which indicates that a request was not successful due to a network error](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful).
56+
However, beware that `fetch` may throw `TypeError` with different error messages on different platforms for similar situations. See [whatwg/fetch#526 (comment)](https://github.com/whatwg/fetch/issues/526#issuecomment-554604080)."
5657
5758
@param input - Receives the number of attempts as the first argument and is expected to return a `Promise` or any value.
5859
@param options - Options are passed to the [`retry`](https://github.com/tim-kos/node-retry#retryoperationoptions) module.

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const pRetry = (input, options) => new Promise((resolve, reject) => {
4848
if (error instanceof AbortError) {
4949
operation.stop();
5050
reject(error.originalError);
51-
} else if (error instanceof TypeError) {
51+
} else if (error instanceof TypeError && error.message !== 'Failed to fetch') {
5252
operation.stop();
5353
reject(error);
5454
} else {

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const run = async () => {
3838

3939
Returns a `Promise` that is fulfilled when calling `input` returns a fulfilled promise. If calling `input` returns a rejected promise, `input` is called again until the maximum number of retries is reached. It then rejects with the last rejection reason.
4040

41-
It doesn't retry on `TypeError` as that's a user error.
41+
42+
It doesn't retry on `TypeError` as that's a user error. The only exclusion to this logic is when `TypeError` is thrown by `fetch`'s API with the message 'Failed to fetch', [which indicates that a request was not successful due to a network error](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful). However, beware that `fetch` may throw `TypeError` with different error messages on different platforms for similar situations. See [whatwg/fetch#526 (comment)](https://github.com/whatwg/fetch/issues/526#issuecomment-554604080)."
4243

4344
#### input
4445

test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ test('no retry on TypeError', async t => {
4848
t.is(i, 1);
4949
});
5050

51+
test('retry on TypeError - failed to fetch', async t => {
52+
const typeErrorFixture = new TypeError('Failed to fetch');
53+
let index = 0;
54+
55+
const returnValue = await pRetry(async attemptNumber => {
56+
await delay(40);
57+
index++;
58+
return attemptNumber === 3 ? fixture : Promise.reject(typeErrorFixture);
59+
});
60+
61+
t.is(returnValue, fixture);
62+
t.is(index, 3);
63+
});
64+
5165
test('AbortError - string', t => {
5266
const error = new pRetry.AbortError('fixture').originalError;
5367
t.is(error.constructor.name, 'Error');

0 commit comments

Comments
 (0)