Skip to content

Commit f011d2e

Browse files
committed
Fix TypeError retry handling
1 parent 85cdece commit f011d2e

4 files changed

Lines changed: 24 additions & 7 deletions

File tree

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ Returns a `Promise` that is fulfilled when calling `input` returns a fulfilled p
221221
222222
Does not retry on most `TypeErrors`, with the exception of network errors. This is done on a best case basis as different browsers have different [messages](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful) to indicate this. See [whatwg/fetch#526 (comment)](https://github.com/whatwg/fetch/issues/526#issuecomment-554604080)
223223
224+
Non-network `TypeError`s always abort retries, even if `shouldConsumeRetry` or `shouldRetry` would otherwise allow another attempt.
225+
224226
@param input - Receives the number of attempts as the first argument and is expected to return a `Promise` or any value.
225227
@param options - Options for configuring the retry behavior.
226228

index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,7 @@ async function onAttemptFailure({error, attemptNumber, retriesConsumed, startTim
129129
}
130130

131131
if (normalizedError instanceof TypeError && !isNetworkError(normalizedError)) {
132-
if (consumeRetry) {
133-
throw normalizedError;
134-
}
135-
136-
options.signal?.throwIfAborted();
137-
return false;
132+
throw normalizedError;
138133
}
139134

140135
if (!await options.shouldRetry(context)) {

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Returns a `Promise` that is fulfilled when calling `input` returns a fulfilled p
3737

3838
Does not retry on most `TypeErrors`, with the exception of network errors. This is done on a best case basis as different browsers have different [messages](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful) to indicate this. See [whatwg/fetch#526 (comment)](https://github.com/whatwg/fetch/issues/526#issuecomment-554604080)
3939

40+
Non-network `TypeError`s always abort retries, even if `shouldConsumeRetry` or `shouldRetry` would otherwise allow another attempt.
41+
4042
#### input
4143

4244
Type: `Function`

test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@ test('shouldRetry is not called for non-network TypeError', async t => {
8282
t.is(shouldRetryCalled, 0);
8383
});
8484

85+
test('does not retry non-network TypeError when shouldConsumeRetry returns false', async t => {
86+
const typeErrorFixture = new TypeError('type-error-fixture');
87+
let attempts = 0;
88+
89+
await t.throwsAsync(pRetry(async () => {
90+
attempts++;
91+
throw typeErrorFixture;
92+
}, {
93+
shouldConsumeRetry() {
94+
return false;
95+
},
96+
retries: 5,
97+
minTimeout: 0,
98+
}), {is: typeErrorFixture});
99+
100+
t.is(attempts, 1);
101+
});
102+
85103
test('retry on TypeError - failed to fetch', async t => {
86104
const typeErrorFixture = new TypeError('Failed to fetch');
87105
let index = 0;
@@ -1362,7 +1380,7 @@ test.serial('Only consumed retries advance backoff', async t => {
13621380
attempts++;
13631381

13641382
if (attempts === 1) {
1365-
throw new TypeError('skip');
1383+
throw new Error('skip');
13661384
}
13671385

13681386
if (attempts === maxAttempts) {

0 commit comments

Comments
 (0)