softonic / axios-retry

Axios plugin that intercepts failed requests and retries them whenever possible
Other
1.9k stars 167 forks source link

How to handle errors #263

Closed alexblack closed 9 months ago

alexblack commented 10 months ago

I've got code like this, where any network errors are meant to be caught:

  try {
    const response = await axios.request(config);
    if (response.status !== 200) {
      throw new Error(
        `Unexpected status tracking event to our server ${response.status}`
      );
    }
  } catch (err) {
    console.error(err);
  }

But it seems when using axios-retry, after some retries, an error is thrown and its unhandled.

Ideally I'd love if my catch statement caught the final error.

My config:

axiosRetry(axios, {
  retryDelay: axiosRetry.exponentialDelay,
  onRetry: (retryCount, err, config) => {
    console.warn(
      `Retrying axios request for ${config.url} ${retryCount}: ${err.message}`
    );
  },
});
yutak23 commented 9 months ago

Are you looking for the code in the catch block to be executed when an error occurs after retries, rather than entering the block where response.status !== 200?

alexblack commented 9 months ago

I think I misunderstood what was happening here, will close the issue! Thanks