Skip to content

Commit 3c22035

Browse files
committed
Fix error for invalid fractional retry counts
Closes #100
1 parent 35681f6 commit 3c22035

4 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎index.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export type Options = {
138138
readonly shouldConsumeRetry?: (context: RetryContext) => boolean | Promise<boolean>;
139139

140140
/**
141-
The maximum amount of times to retry the operation.
141+
The maximum amount of times to retry the operation. Must be a non-negative integer or `Infinity`.
142142
143143
@default 10
144144
*/

‎index.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ function validateRetries(retries) {
99
if (Number.isNaN(retries)) {
1010
throw new TypeError('Expected `retries` to be a valid number or Infinity, got NaN.');
1111
}
12+
13+
if (Number.isFinite(retries) && !Number.isInteger(retries)) {
14+
throw new TypeError('Expected `retries` to be a non-negative integer or Infinity.');
15+
}
1216
} else if (retries !== undefined) {
1317
throw new TypeError('Expected `retries` to be a number or Infinity.');
1418
}

‎readme.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ In the example above, `RateLimitError`s will not decrement the available `retrie
164164
Type: `number`\
165165
Default: `10`
166166

167-
The maximum amount of times to retry the operation.
167+
The maximum amount of times to retry the operation. Must be a non-negative integer or `Infinity`.
168168

169169
##### factor
170170

‎test.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,19 @@ test('throws on NaN retries', async t => {
932932
);
933933
});
934934

935+
test('throws on fractional retries', async t => {
936+
let inputCalled = false;
937+
938+
await t.throwsAsync(pRetry(() => {
939+
inputCalled = true;
940+
}, {retries: 0.5}), {
941+
instanceOf: TypeError,
942+
message: 'Expected `retries` to be a non-negative integer or Infinity.',
943+
});
944+
945+
t.false(inputCalled);
946+
});
947+
935948
test('handles zero retries', async t => {
936949
let attempts = 0;
937950

0 commit comments

Comments
 (0)