Skip to content
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

Add callback option when it retries #25

Closed
shabailiu opened this issue Nov 9, 2017 · 3 comments
Closed

Add callback option when it retries #25

shabailiu opened this issue Nov 9, 2017 · 3 comments

Comments

@shabailiu
Copy link

It would be useful to have a callback function whenever axios retries, for example to update UI status.

@recz5
Copy link

recz5 commented Dec 16, 2017

I'm working on an existing code based that has axios-retry implemented and it is hard to debug. It is difficult to identify if it is retrying. A callback would be helpful.

@rubennorte
Copy link
Contributor

I think you could use a different interceptor to detect retries. You just need to register it after axios-retry and check the retry count. We're trying to keep the API minimal, and this use case can be implemented without adding new options.

@bbugh
Copy link

bbugh commented Mar 21, 2019

If anyone else ends up here trying to answer this question, here's an implementation based on the suggestion to use interceptors. The example is in TypeScript but it will be the same in JS, just remove the types.

This gets the count of the tries and logs a warning:

interface AxiosRetryRequestConfig extends AxiosRequestConfig {
  'axios-retry'?: {
    retryCount: number,
    lastRequestTime: number
  }
}

axios.interceptors.request.use((config: AxiosRequestConfig) => {
  const retryRequestData = (<AxiosRetryRequestConfig>config)['axios-retry']

  if (retryRequestData) {
    const retryCount = retryRequestData.retryCount
    logger.warn(`[webhook] Webhook call retry #${retryCount}: ${config.url}`)
  }

  return config
})

My logger output:

2019-03-21 14:23:00 [info]:     [webhook] Sending webhook to http://localhost/webhooks/somewhere
2019-03-21 14:23:02 [warn]:     [webhook] Webhook call retry #1: http://localhost/webhooks/somewhere
2019-03-21 14:23:05 [warn]:     [webhook] Webhook call retry #2: http://localhost/webhooks/somewhere
2019-03-21 14:23:06 [info]:     [webhook] Response from webhook call: 200 OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants