softonic / axios-retry

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

Inconsistent Behavior of `isNetworkOrIdempotentRequestError` Function #254

Closed meirkeller closed 1 year ago

meirkeller commented 1 year ago

Hello,

I've been using the axios-retry module and I've noticed an inconsistent behavior with the isNetworkOrIdempotentRequestError function. It seems that sometimes the same error is handled by this function, and sometimes it's not.

This inconsistency is causing some issues in my application as I rely on this function to handle certain network or idempotent request errors. I'm not sure if this is a bug or if I'm missing something in my implementation.

For reference, Here is the relevant code snippet:

// Server
const fastify = require('fastify')({
  logger: true,
});

fastify.get('/', function () {
  const isHealthy = Math.random() > 0.5;
  if (!isHealthy) {
    throw new Error();
  }
  return { hello: 'world' };
});

fastify.post('/', function () {
  const isHealthy = Math.random() > 0.5;
  if (!isHealthy) {
    throw new Error();
  }
  return { hello: 'world' };
});

fastify.listen({ port: 3000 }, function (err) {
  if (err) {
    fastify.log.error(err);
    process.exit(1);
  }
});
// Client
const axios = require('axios');
const axiosRetry = require('axios-retry');

const instance = axios.create({
  baseURL: 'http://localhost:3000',
});

axiosRetry(instance, {
  retries: 5,
  retryDelay: axiosRetry.exponentialDelay,
  onRetry: (retryCount, error, requestConfig) => {
    console.log(`Retry attempt #${retryCount} for ${requestConfig.url}, error: ${error.message}`);
  },
  retryCondition: (error) => {
    console.log(`Condition Result: ${axiosRetry.isNetworkOrIdempotentRequestError(error)}`);
    return axiosRetry.isNetworkOrIdempotentRequestError(error);
  },
});

(async () => {
  async function get () {
    const { data } = await instance.get('/');
    return data;
  }

  async function post () {
    const { data } = await instance.post('/', {
      payload: 'test',
    });
    return data;
  }

  try {
   console.log(await get());
    console.log(await post());
  } catch (error) {
    console.log(error.message);
  }
})();

The most important log to pay attention to is

console.log(`Condition Result: ${axiosRetry.isNetworkOrIdempotentRequestError(error)}`);

Sometimes the output is Condition Result: false and sometimes Condition Result: true Could you please provide some guidance on how this function is supposed to work? Any help would be greatly appreciated.

Thank you

meirkeller commented 1 year ago

I've found the answer for this https://developer.mozilla.org/en-US/docs/Glossary/Idempotent. GET and POST retries act differently.