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

Don't error when trying to extend non-extensible errors #83

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ const decorateErrorWithCounts = (error, attemptNumber, options) => {
// Minus 1 from attemptNumber because the first attempt does not count as a retry
const retriesLeft = options.retries - (attemptNumber - 1);

error.attemptNumber = attemptNumber;
error.retriesLeft = retriesLeft;
try {
error.attemptNumber = attemptNumber;
error.retriesLeft = retriesLeft;
} catch {
// ignore errors trying to extend non-extensible errors
}

return error;
};

Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,17 @@ test('should retry only when shouldRetry returns true', async t => {

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

test('can retry functions that throw non-extensible errors', async t => {
let index = 0;
const nonExtensibleError = Object.preventExtensions(new Error('non-extensible-error'));

const returnValue = await pRetry(async attemptNumber => {
await delay(40);
index++;
return attemptNumber === 3 ? fixture : Promise.reject(nonExtensibleError);
});

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