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

feat: add no throw status codes option in retry agent #4082

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sydo26
Copy link

@sydo26 sydo26 commented Mar 5, 2025

This relates to...

#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

@mcollina
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants