Retry Case Gives Timeout with Axios

Asked

Viewed 2,885 times

4

Eae guys, I have a little problem to give a Retry in a POST using Next case exceeds Timeout. I’m trying to implement this(https://github.com/softonic/axios-retry) library next to Axios, but it doesn’t work. Let’s go to the code:

const axios = require('axios')
const convert = require('xml-js')
const axiosRetry = require('axios-retry');


const CLIENT = axios.create();
CLIENT.defaults.timeout = 1000
axiosRetry(CLIENT, {
    retryCondition: (error) => {
        return axiosRetry.isNetworkOrIdempotentRequestError(error) || error.code === 'ECONNABORTED';
    }
});

class Consult {
    constructor(username, password) {
        this.username = username
        this.password = password
    }    

    async getResponse() {
        const config = {
            headers: {
                'Content-Type': 'text/xml'
            }
        };
        const xml = 'string_conection'

        return await CLIENT.post('url', xml, config)
        .then(response => {
            const result = convert.xml2json(response.data, {
                compact: true,
                spaces: 4
            })
            return result
        })
        .catch(error => {
            console.log(error.code);
        });
    }
}

module.exports = Consult

Who can help me :)

2 answers

6

I created my own function to retry:

const axios = require('axios');

/**
 * Waits a determined time to fulfill a Promise
 *
 * @param {number} ms - The milliseconds to fulfill the Promise
 *
 * @returns {Promise<any>} Represents the fulfilled time
 */
const sleep = (ms) => new Promise((res) => setTimeout(res, ms));

/**
 * Try to request the following URL using the maximumRetry option
 *
 * @param {string} url - The URL to connect
 * @param {object} options - The options to pass to axios
 * @param {number} attempt - The current attempt number
 * @param {number} delay - The time in milliseconds to wait before request
 *
 * @returns {Promise<*|void>} Represents the fulfilled request
 *
 * @private
 */
const retry = async (url, options, maximumRetry = 0, attempt = 0, delay = 0) => {
  try {
    await sleep(delay);
    const { data } = await axios.request({ url, ...options });

    return data;
  } catch (e) {
    if (attempt >= maximumRetry) throw e;

    return retry(url, options, attempt + 1, (delay || 1000) * 2);
  }
};

And the use is as follows for 3 attempts, for example:

const response = await retry(url, {
  data,
  headers,
  httpsAgent,
  method: 'POST',
  timeout: opts.timeout,
}, 3);
  • Hmm, good. It turns out that yesterday I also created my own function. I don’t know if I can post it here as an answer to help people...

  • @Develop_sp can yes, it is only important that you accept some answer as chosen so that the question stays in the normal flow, even if it is your own

1

You can intercept it request of Xios where there is an error and the Http Status Code either timeout (408) Voce tries again.

Follows implementation of Xios interceptors

Example:

        axios.onError((res) => {
            if (res.response.status === 408) {
                console.info(`Tentar novamente`, res.config)
                return axios.request(res.config)
            }
        }),

Browser other questions tagged

You are not signed in. Login or sign up in order to post.