0

I have made axios post request but my server couldn't handle the request so it returns some error. In such cases, I need to make the request again in my axios response interceptor. Any easy way to do

sundar
  • 124
  • 10
  • Did you check this https://stackoverflow.com/questions/942951/rest-api-error-return-good-practices?rq=1? – krankuba Apr 23 '18 at 09:00

1 Answers1

2

You can try this:

axios.interceptors.response.use(undefined, (err) => {
  const count = (err.config || {}).retryCount;
  if (count > 0) { 
    return axios({ ...err.config, retryCount: count - 1 });
  }
  throw err;
});

axios.get('/', { retryCount: 3 });

Add in && err.status === ... if you only want to retry for certain errors (probably only server errors rather than client errors).

Roy Wang
  • 11,112
  • 2
  • 21
  • 42