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

retryCondition not called anymore on HTTP 401 starting from axios-retry v3.2.0 #240

Open
bennycode opened this issue Aug 14, 2023 · 6 comments

Comments

@bennycode
Copy link

I recently tried to upgrade my axios-retry v3.1.9 but any newer version (starting from 3.2.0) has a behaviour change. In v3.1.9 (used together with axios v1.4.0) my axiosRetry gets called on a HTTP 401 error:

axiosRetry(this.httpClient, {
  retries: Infinity,
  retryCondition: (error: AxiosError) => {
    const errorCode = hasErrorCode(error) ? error.response!.data.errorCode : undefined;

    switch (errorCode) {
      case 'error.public-api.exceeded-api-key-allowance':
        // Got rate limited
        return true;
      case 'error.security.oauth-token-invalid':
        // Security token expired
        void this.login.refreshToken();
        return true;
      case 'error.security.client-token-missing':
        // Trading session has not been initialized (HTTP 401, gets called using axios-retry v3.1.9)
        const {username, password} = this.auth;
        if (username && password) {
          void this.login.createSession(this.auth.username, this.auth.password);
          return true;
        }
        throw new Error(
          `Cannot fulfill request because there is no active session and username & password have not been provided.`
        );
      default:
        return true;
    }
  },
  retryDelay: axiosRetry.exponentialDelay,
});

I noticed when I upgrade to axios-retry v3.2.0 (or newer) my axiosRetry doesn't get called anymore on a HTTP 401 error. Instead the axios call fails and returns a generic 'Request failed with status code 401' error message.

@mindhells was there any behaviour change in axios-retry that could cause this? Is this newly added shouldRetry function intercepting it?

Here is how my test is set up: https://github.com/bennycode/ig-trading-api/blob/v0.13.5/src/dealing/DealingAPI.test.ts#L474-L517

Best,
Benny

@bennycode bennycode changed the title Noticed behaviour change starting from axios-retry v3.2.0 retryCondition not called anymore on HTTP 401 starting from axios-retry v3.2.0 Aug 14, 2023
@yutak23
Copy link
Contributor

yutak23 commented Aug 15, 2023

I don't know in which commit the change was made, etc., but it seems to me that not retrying on 401 is the way the spec should be.

Since 401 is an Unauthorized error, I don't think it should be retried, as it is certain to be an error no matter how many times it is retried.

@mindhells
Copy link
Member

I recently tried to upgrade my axios-retry v3.1.9 but any newer version (starting from 3.2.0) has a behaviour change. In v3.1.9 (used together with axios v1.4.0) my axiosRetry gets called on a HTTP 401 error:

axiosRetry(this.httpClient, {
  retries: Infinity,
  retryCondition: (error: AxiosError) => {
    const errorCode = hasErrorCode(error) ? error.response!.data.errorCode : undefined;

    switch (errorCode) {
      case 'error.public-api.exceeded-api-key-allowance':
        // Got rate limited
        return true;
      case 'error.security.oauth-token-invalid':
        // Security token expired
        void this.login.refreshToken();
        return true;
      case 'error.security.client-token-missing':
        // Trading session has not been initialized (HTTP 401, gets called using axios-retry v3.1.9)
        const {username, password} = this.auth;
        if (username && password) {
          void this.login.createSession(this.auth.username, this.auth.password);
          return true;
        }
        throw new Error(
          `Cannot fulfill request because there is no active session and username & password have not been provided.`
        );
      default:
        return true;
    }
  },
  retryDelay: axiosRetry.exponentialDelay,
});

I noticed when I upgrade to axios-retry v3.2.0 (or newer) my axiosRetry doesn't get called anymore on a HTTP 401 error. Instead the axios call fails and returns a generic 'Request failed with status code 401' error message.

@mindhells was there any behaviour change in axios-retry that could cause this? Is this newly added shouldRetry function intercepting it?

Here is how my test is set up: https://github.com/bennycode/ig-trading-api/blob/v0.13.5/src/dealing/DealingAPI.test.ts#L474-L517

Best, Benny

Not that I'm aware of, but as @yutak23 pointed out, this is the intended behaviour

I don't remember which ones, but there are a couple of issues discussing about this and how to workaround

@younes-zeboudj
Copy link

I don't know in which commit the change was made, etc., but it seems to me that not retrying on 401 is the way the spec should be.

Since 401 is an Unauthorized error, I don't think it should be retried, as it is certain to be an error no matter how many times it is retried.

Some actor may not follow the standards, I think letting the choice to the user is more suitable, as such we can still reproduce how the spec should be and have more flexibility at the same time.

@yutak23
Copy link
Contributor

yutak23 commented Sep 24, 2023

I think letting the choice to the user is more suitable, as such we can still reproduce how the spec should be and have more flexibility at the same time.

If the default spec is unchangeable, which I agree would be a major problem.
However, it is only that the default spec does not retry 401, and it is possible to implement a custom 401 retry. In that sense, I think choice to the user is already satisfied.

I have a custom implementation as well. Specifically, the default is to retry 503, but that was problematic in my application, so I implemented not to retry 503.

@devfacet
Copy link

devfacet commented Oct 1, 2023

@bennycode : Have you found a solution for your case?

@bennycode
Copy link
Author

@devfacet no, I haven't. The PR to upgrade axios-retry from 3.1.9 to 3.8.0 is failing because of the bug reported here.

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

No branches or pull requests

5 participants