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.

Adding Retry Parameter

See original GitHub issue

I have one API that time to time returns ECONNRESET. So I’d like to do retry on such an error. Do you have plans to add retry feature to this library?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:23 (4 by maintainers)

github_iconTop GitHub Comments

249reactions
KyleRosscommented, Sep 7, 2017

@mericsson I am too in need of exponential backoff when retrying. I’ve put together the following which works great. You my want to add in checking for specific errors/status codes as this currently just intercepts and retries all errors.

axios.interceptors.response.use(undefined, function axiosRetryInterceptor(err) {
    var config = err.config;
    // If config does not exist or the retry option is not set, reject
    if(!config || !config.retry) return Promise.reject(err);
    
    // Set the variable for keeping track of the retry count
    config.__retryCount = config.__retryCount || 0;
    
    // Check if we've maxed out the total number of retries
    if(config.__retryCount >= config.retry) {
        // Reject with the error
        return Promise.reject(err);
    }
    
    // Increase the retry count
    config.__retryCount += 1;
    
    // Create new promise to handle exponential backoff
    var backoff = new Promise(function(resolve) {
        setTimeout(function() {
            resolve();
        }, config.retryDelay || 1);
    });
    
    // Return the promise in which recalls axios to retry the request
    return backoff.then(function() {
        return axios(config);
    });
});

To use:

axios.get('/some/endpoint', { retry: 5, retryDelay: 1000 })
    .then(function(res) {
        console.log('success', res.data);
    })
    .catch(function(err) {
        console.log('failed', err);
    });

Config Options: retry - Number of times to retry the request after first failed request. retryDelay - Number of milliseconds to wait in between failed requests (defaults to 1).

I may make this a bit more configurable in a gist at some point.

58reactions
jtangeldercommented, Dec 22, 2015
function retryFailedRequest (err) {
  if (err.status === 500 && err.config && !err.config.__isRetryRequest) {
    err.config.__isRetryRequest = true;
    return axios(err.config);
  }
  throw err;
}
axios.interceptors.response.use(undefined, retryFailedRequest);

Something like this! It retries 500 errors once.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Retry parameters for HTTP HTTP(s) Transport
Procedure ; Retry Count, Set the value to use as a number of retries for HTTP connection. ; Retry Interval, Set the value...
Read more >
Set Retry Parameters - Syniti
Set Retry Parameters · Select Interfaces in the Navigation pane. · Click the Events icon for the desired interface. · Click Vertical View...
Read more >
Guide to Spring Retry - Baeldung
A quick and practical guide to implementing retry logic with Spring Retry.
Read more >
How to Implement Retry Logic in C# - Code Maze
In this article, we are going to learn how to implement retry logic in C# to help us handle problems that we can...
Read more >
Retry decorator in Python - Medium
api/status , takes the parameter “id” returned from api/getData , and checks ... To make any function as a retry logic , just...
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