question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Won't retry more than once

See original GitHub issue

EDIT: I swapped retry-axios for axios-retry @ https://github.com/softonic/axios-retry , using the same logical flow and it works fine with proper repeats. What’s so different about retry-axios’ implementation?

Hello.

I’ve been trying to get this wonderful interceptor all day but I just can’t get it to work with my React app.

I have the following code.

export default{
  get(url, conf = {}) {
    const client = getClient();
    client.defaults.raxConfig = {
      retry: 5,
      instance: client,
      noResponseRetries: 5,
      statusCodesToRetry: [[100, 199], [400, 429],[500, 599]],
      onRetryAttempt: (err) => {
        const cfg = rax.getConfig(err);
        console.log(`Retry attempt #${cfg.currentRetryAttempt}`);
      }
    }
    const interceptorId = rax.attach(client)
    return client.get(url, conf)
  }
}

Basically another apiCall.js file calls the exported function, it creates an axios instance with some basic options via getClient(), attaches the raxConfig, attaches rax itself and then returns the get request.

The apiCall.js file then resolves the promises via .then() and .catch().

The problem im experiencing is no matter what I do retry-axios only retries once.

I know it uses the config because it properly reacts to changes in statusCodesToRetry, but doesn’t do anything after the first retry.

I’m really desperate here and any help is appreciated. Thank you.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
ukcrpb6commented, May 1, 2019

Had a similar issue, this was due to the use of axios defaults which is copied by reference, not cloned to each request. This meant the number of current retries stored in the config by the request error handler is global, leading to effectively 1 set of retries.

This can be fixed using a request interceptor to set the config instead such as the following (typescript):

import { AxiosInstance, AxiosRequestConfig } from 'axios';
import { attach, RetryConfig } from 'retry-axios';

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type PartialRaxConfig = { raxConfig?: RetryConfig } & AxiosRequestConfig;

export function applyAxiosRetry(
  instance: AxiosInstance,
  options?: Omit<RetryConfig, 'instance'>,
): AxiosInstance {
  instance.interceptors.request.use((config: PartialRaxConfig) => {
    config.raxConfig = config.raxConfig || { instance, ...options };
    return config;
  });

  attach(instance);
  return instance;
}

usage:

import axios from 'axios';
import { applyAxiosRetry } from './axios-retry.utils';

const instance = axios.create({ /* ... */ });
applyAxiosRetry(instance, {
  retry: 5,
  /* ... */
});

relates to #1

3reactions
erzzocommented, Feb 22, 2019

Okay, my problem was that in the config, httpMethodsToRetry didn’t include the POST method by default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Retry always retry one time more than asked in test
You should think about first run because when you retry 5 times and run once again. It should be 6 run even if...
Read more >
Retry logic needs different kinds of max retries #960 - GitHub
Is there value to retrying a clock skew error more than once? Maybe the solution is to only retry clock skew errors once...
Read more >
Retries and Timeouts | Linkerd
Timeouts work hand in hand with retries. Once requests are retried a certain number of times, it becomes important to limit the total...
Read more >
Retrying Failing Tests - JUnit Pioneer
The test requiresTwoSuccesses must run at least two times without raising an exception. However, it will not run more than four times. Suspending...
Read more >
Error handling and automatic retries in AWS Lambda
Asynchronous invocation – Lambda retries function errors twice. If the function doesn't have enough capacity to handle all incoming requests, events might ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found