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

retry on all thrown errors by default #90

Merged
merged 5 commits into from
May 5, 2022
Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ Without configuring any parameters, the retry behavior will be as follows:
- retry for 60s
- retry inital delay of 100ms with exponential backoff, configurable as a multiplier defaulting to 2
- retry only on 5xx response
- retry on all FetchError system errors
- retry on all errors thrown by fetch
- see node-fetch error handling: https://github.com/node-fetch/node-fetch/blob/main/docs/ERROR-HANDLING.md
- with special behavior to avoid retrying on developer errors (program errors)
- this includes `AbortError`'s, `FetchError`'s
- socket timeout of 30s
```js
const fetch = require('@adobe/node-fetch-retry');
Expand Down
21 changes: 1 addition & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function retryInit(options={}) {
retryOnHttpResponse: ((typeof retryOptions.retryOnHttpResponse === 'function') && retryOptions.retryOnHttpResponse) ||
((response) => { return response.status >= 500; }),
retryOnHttpError: ((typeof retryOptions.retryOnHttpError === 'function') && retryOptions.retryOnHttpError) ||
((error) => { return shouldRetryOnHttpError(error); }),
(() => { return true; }),
socketTimeout: socketTimeoutValue
};
}
Expand Down Expand Up @@ -146,25 +146,6 @@ function checkParameters(retryOptions) {
}
}

/**
* Evaluates whether or not to retry based on HTTP error
* @param {Object} error
* @returns Returns true for all FetchError's of type `system`
*/
function shouldRetryOnHttpError(error) {
// special handling for known fetch errors: https://github.com/node-fetch/node-fetch/blob/main/docs/ERROR-HANDLING.md
// retry on all errors originating from Node.js core
// retry on AbortError caused by network timeouts
if (error.name === 'FetchError' && error.type === 'system') {
console.error(`FetchError failed with code: ${error.code}; message: ${error.message}`);
return true;
} else if (error.name === 'AbortError') {
console.error(`AbortError failed with type: ${error.type}; message: ${error.message}`);
return true;
}
return false;
}

/**
* @typedef {Object} RetryOptions options for retry or false if want to disable retry
* @property {Integer} retryMaxDuration time (in milliseconds) to retry until throwing an error
Expand Down
Loading