softonic / axios-retry

Axios plugin that intercepts failed requests and retries them whenever possible
Other
1.9k stars 167 forks source link

Axios interceptor logging issue #158

Open JeremyBradshaw7 opened 3 years ago

JeremyBradshaw7 commented 3 years ago

Trying this library for the first time in a react-native project. I'm finding that quite often an API call is retried once, twice or even 3 times before the original call has even returned a response, so it's initiating retries in parallel.

    axiosRetry(this.secureApi, {
      retries: 3,
      retryCondition: (e) => {
        const retry = isNetworkOrIdempotentRequestError(e);
        logger('/// AXIOS RETRY?', retry);
        return retry;
      }
    });

See here it's retrying two of the requests though nothing is failing: image

Anyone any idea what's going on?

JeremyBradshaw7 commented 3 years ago

I've logged another instance this time logging the error response in the retryCondition function: image

I now think axios-retry is working properly on a genuine failure but it's messing up the logging of the requests I'm doing via axios request and response interceptors (so I've changed the title of this issue accordingly):

      this.secureApi.interceptors.request.use((config) => {
        logger('%c ' + config.method.toUpperCase() + ' ' + config.url + ' ', 'background-color: #0086b3; color: #ffffff;', config.data, config);
        return config;
      }, (error) => {
        // logger('Error', error);
        return Promise.reject(error);
      });

      this.secureApi.interceptors.response.use((response) => {
        logger('< %c ' + (response ? response.status : '?') + ' ', 'background-color: #0086b3; color: #ffffff;', response.config.url, response.data, response);
        return response;
      }, (error) => {
        logger('<< %c ' + (error.response ? error.response.status : '?') + ' ', 'background-color: #a71d5d; color: #ffffff;', error.config.url, error.response);
        this.logError(error);
        return Promise.reject(error);
      });

Why would this show both original and retried requests both with a success response after the retried request succeeds? Is it something to do with the fact that the original request promise is eventually satisfied by the retry? Can I prevent the axios response interceptor firing on the original failed response, or at least make it show the true failure rather than the retried success?

rosshudgins commented 3 years ago

I think I'm seeing this issue as well. I have a response interceptor in order to calculate some metrics, but for retried calls I'm seeing the same metric log twice.

nirpeer commented 2 years ago

I'm also getting the success response twice in the axios.interceptors.response, and not one failure response and a successful one. How can we deal with this issue?

By the way, the request interceptor also fired twice, but i write different logs with the attempt number, so in that case it's great. But getting the same valid response twice on a failure+success responses is not that nice :)