axios-retry not working as expected. What is wrong with my config?

7.1k views Asked by At

I have configured axios-retry in my nodejs application as per https://www.npmjs.com/package/axios-retry

Following is my code

import axios from 'axios';
import axiosRetry from 'axios-retry';


export class RetryRoute {

  public async testRetry(
    req: express.Request,
    res: express.Response,
    next: express.NextFunction,
 ): Promise<any> {
     const client = axios.create({ baseURL: `http://www.test.com/` });
     axiosRetry(axios, { retries: 3 });

     client.get('/error') 
        .then(result => {
            this.logger.info('success', result);
            result.data; 
        }).catch(error => { 
            console.log('error', error.message);
            error !== undefined
        });
 }
}

console.log('error', error.message);. prints as expected. which means call is failed with code 404 as expected. But next call from retry not happening.

2

There are 2 answers

0
Prasad Parab On

You need to specify retry condition, below code retry for every failed request

 axiosRetry(axios, {
 retries: 3,
 retryCondition: () => true
 });
1
Adam Burke On

From https://github.com/softonic/axios-retry

By default, it retries if it is a network error or a 5xx error on an idempotent request (GET, HEAD, OPTIONS, PUT or DELETE).

A http 404 status code means the request was handled successfully but was invalid. It doesn't retry in this case as it would expect the retry to also fail presumably with almost 100% certainty. 404 errors shouldn't be transient in the same way 500 errors or network errors may be.