Gnik
Gnik

Reputation: 7428

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

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.

Upvotes: 7

Views: 9533

Answers (2)

Adam Burke
Adam Burke

Reputation: 37

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.

Upvotes: 2

Prasad Parab
Prasad Parab

Reputation: 496

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

 axiosRetry(axios, {
 retries: 3,
 retryCondition: () => true
 });

Upvotes: 5

Related Questions