undici icon indicating copy to clipboard operation
undici copied to clipboard

feat: add no throw status codes option in retry agent

Open sydo26 opened this issue 10 months ago • 1 comments

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 noThrowStatusCodes prevents the error from being thrown on the last attempt (if the conditions are met), the retryFn will not be executed in this situation.
  • If noThrowStatusCodes is not used, the behavior of the retryFn remains unchanged.

Changes

  • Added the noThrowStatusCodes option to RetryHandlerOptions.
  • Added the noThrowStatusCodes option to the retryOpts of the RetryHandler.
  • 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

sydo26 avatar Mar 05 '25 05:03 sydo26

I'm sorry this now conflicts, can you update?

mcollina avatar Mar 12 '25 16:03 mcollina