undici
undici copied to clipboard
feat: add no throw status codes option in retry agent
This relates to...
https://github.com/nodejs/undici/issues/4080
Rationale
This PR adds the noThrowStatusCodes option to the RetryHandler, allowing you to configure an array of HTTP status codes that should not trigger a RequestRetryError on the last attempt.
Expected Behavior
- If the HTTP status code of the response is in
noThrowStatusCodes, the error will not be thrown (only on the last attempt), allowing the response to be processed normally. - This option only interferes if the status code is greater than or equal to 300 and is also listed in
statusCodes.
Notes on the retryFn callback
The last attempt considered is determined by the value of maxRetries. However, if you are using a retryFn to manage your own retries, there is an important detail:
- The retryFn is only called when an error occurs in the handler. If no error is thrown, retryFn will not be executed.
- Since
noThrowStatusCodesprevents the error from being thrown on the last attempt (if the conditions are met), theretryFnwill not be executed in this situation. - If
noThrowStatusCodesis not used, the behavior of theretryFnremains unchanged.
Changes
- Added the
noThrowStatusCodesoption toRetryHandlerOptions. - Added the
noThrowStatusCodesoption to theretryOptsof theRetryHandler. - Added the condition to not throw an error on the last attempt if the response status code is in
noThrowStatusCodes. - Added tests to ensure that the error is not thrown on the last attempt if the response status code is in
noThrowStatusCodes.
Features
// Ignore last attempt throw error if status code is 503.
// No ignore last attempt throw error if status code is 500.
const retryAgent = new RetryAgent({
noThrowStatusCodes: [503], // <-- Add this option
statusCodes: [503, 500],
})
Bug Fixes
Breaking Changes and Deprecations
Status
- [x] I have read and agreed to the Developer's Certificate of Origin
- [x] Tested
- [ ] Benchmarked (optional)
- [ ] Documented
- [x] Review ready
- [ ] In review
- [ ] Merge ready
I'm sorry this now conflicts, can you update?