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
Adding Retry Parameter #164
Comments
any update on this? I think this is very important. The scenario maybe when we implement Axios for mobile web, then the user switch the internet source from WiFi to any kind of mobile connection. |
I don't think this kind of functionality should be in this library. If you need this, you can create a interceptor for this. |
@jtangelder any ideas how should it look like? |
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. |
I agree with @jtangelder that this doesn't belong in the core library. This is a great use case for interceptors as illustrated. |
@jtangelder @mzabriskie thanks! actually it is great example for documentation too |
+1 I wish this example was easier to find! |
@jtangelder thanks a lot for the snippet A little correction on the snippet, I think the returned error status should be 504 instead of 500. |
@jtangelder very helpful example. I'm wondering, any ideas on how to do something with exponential backoff? Is it possible to return a promise from |
The example in @jtangelder should be included in the cookbook page |
You can now use axios-retry for this. |
I agree. This should really be a part of the library, or at least better-documented. |
@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: I may make this a bit more configurable in a gist at some point. |
@KyleRoss Thanks for the code, I made an adjustment for the retries to be exponential
|
@KyleRoss Thanks for sharing. The delay can be set increasingly:
|
If you want to retry an 4xx or 5xx request, you need work with the error object like that: To intercept your requests:
To intercept your responses:
|
Awesome ideas. I rolled a fair bit of this up into an npm module, if folks are interested: Happy to take feature requests ✨ |
|
@KyleRoss I tried adding the retry and retryDelay configuration options to AxiosRequestConfig but it doesnt look like they exist. How did you get that to work? |
@lawloretienne you need you make sure you add the code in my example prior to using any of the retry parameters. Since it acts more like a plugin, the new properties are not documented as part of axios itself. |
note source from axios/axios#164 (comment)
@KyleRoss Unable to pass config config, each 'config.__reyCount' is undefined. |
@dennisreimann custom config are now filtered out in v0.19, and now we can pass config like this:
|
thanks for help! |
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 addretry
feature to this library?The text was updated successfully, but these errors were encountered: