Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add shouldRetry option #75

Merged
merged 10 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ export type Options = {
*/
readonly onFailedAttempt?: (error: FailedAttemptError) => void | Promise<void>;

/**
Decide if a retry should occur based on the error. Returning true triggers a retry, false aborts with the error.

sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
@param error - The error thrown by the input function.

@example
```
pukuba marked this conversation as resolved.
Show resolved Hide resolved
import pRetry from 'p-retry';

const run = async () => { … };

const result = await pRetry(run, {
shouldRetry: error => !(error instanceof CustomError);
});
```

In the example above, the operation will be retried unless the error is an instance of `CustomError`.
*/
readonly shouldRetry?: (error: FailedAttemptError) => boolean | Promise<boolean>;

/**
You can abort retrying using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).

Expand Down
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ const decorateErrorWithCounts = (error, attemptNumber, options) => {
return error;
};

const shouldRetry = () => true;
pukuba marked this conversation as resolved.
Show resolved Hide resolved

export default async function pRetry(input, options) {
return new Promise((resolve, reject) => {
options = {
onFailedAttempt() {},
retries: 10,
shouldRetry,
...options,
};

Expand Down Expand Up @@ -70,7 +73,14 @@ export default async function pRetry(input, options) {
throw error;
}

await options.onFailedAttempt(decorateErrorWithCounts(error, attemptNumber, options));
decorateErrorWithCounts(error, attemptNumber, options);

if (!(await options.shouldRetry(error))) {
operation.stop();
reject(error);
}

await options.onFailedAttempt(error);

if (!operation.retry(error)) {
throw operation.mainError();
Expand Down
30 changes: 30 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,33 @@ test('preserves the abort reason', async t => {

t.is(index, 3);
});

test('should retry only when shouldRetry returns true', async t => {
t.plan(2);

const shouldRetryError = new Error('should-retry');
const customError = new Error('custom-error');

let index = 0;

try {
await pRetry(
async () => {
await delay(40);
index++;
const error = index < 3 ? shouldRetryError : customError;
throw error;
},
{
async shouldRetry(error) {
return error.message === shouldRetryError.message;
},
retries: 10,
},
);
} catch (error) {
t.is(error.message, customError.message);
pukuba marked this conversation as resolved.
Show resolved Hide resolved
}

t.is(index, 3);
});