Skip to content

Commit

Permalink
fix retry on all thrown fetch errors (#90)
Browse files Browse the repository at this point in the history
* retry on all error by default

* update doc

* requested changes
  • Loading branch information
jdelbick authored May 5, 2022
1 parent 47ec289 commit 9936914
Show file tree
Hide file tree
Showing 4 changed files with 1,757 additions and 4,189 deletions.
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

0 comments on commit 9936914

Please sign in to comment.