JustinBeckwith/retry-axios

Question: How do I use this with existing error interceptors?

DaveStein opened this issue ยท 7 comments

This library looks like exactly what I need. I am trying it out with code like this:

rax.attach();

axios.defaults.raxConfig = {
  onRetryAttempt: err => {
    const cfg = rax.getConfig(err);
    console.log(`Retry attempt #${cfg.currentRetryAttempt}`);
  }
};
axios.interceptors.response.use(interceptors.response.success, interceptors.response.error);`

interceptors.response.error is where I build up custom error for logging, which includes the request & response details. At the top I threw this in:

console.log(e.message);
console.log(typeof e.config);

When I purposefully error via gibberish URL the console.logs output this:

Retry attempt #1
Retry attempt #2
getaddrinfo ENOTFOUND www.asdlkfjaslfkjadflkjfadslfjnafdslkfasd.com www.asdlkfjaslfkjadflkjfadslfjnafdslkfasd.com:80
object
getaddrinfo ENOTFOUND www.asdlkfjaslfkjadflkjfadslfjnafdslkfasd.com www.asdlkfjaslfkjadflkjfadslfjnafdslkfasd.com:80
undefined

How should I ensure that my logic only runs after final attempt AND for any error that has no retries? ie an error with POST, which would never really run onRetryAttempt

Right now it looks to fire your plugin twice followed by my generic handler twice.

PS: If it's not clear, the second call to my generic handler does not receive the configuration object for axios so I can't get the details for my logs. The first does. I only want to log the second.

@JustinBeckwith is this something you can answer or do you think this is more of a Stack Overflow Question? I'm kind of wondering if there's a feature request in here for like "callback for final error" such that you get it after failed number of retries or the failure case for requests that don't get retries.

@JustinBeckwith any suggestion here? ๐Ÿ™

Greetings! Not something I've had time to look into, apologies.

@JustinBeckwith I realize attach is just doing return instance.interceptors.response.use(onFulfilled, onError);

If you exported onError, that would imply I can just use your same logic in my existing interceptor, and on rejection of onError (which I believe happens when the last retry fails), I can grab the error and do what I would usually do.

What do you think of that strategy?

@DaveStein - did you ever find a solution for this?

@danthareja we made our own interceptor that would instantiate a new instance of axios and go from there.