Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

339
Views
axios-retry is not working for response with status 4XX

I am using axios for api calls. Incase some failure happens or response status != 200 I need to retry the api call. By default retry axios works for status with 5XX . But as per documentation we can override retryCondition as per our requirements.

Here is my code snippet

export const doApiFetchCall = (apiEndPoint, dataPayLoad, config, axiosObject, callType,caller,timeoutParam,retryCount) => {
let instance = undefined;
if(axiosObject === 'axios') {
    instance = localAxios;
} else if(axiosObject === 'axiosProxy') {
    instance = localAxiosProxy;
} else if (axiosObject === 'axiosProxyJira') {
    instance = localAxiosProxyJira;
}
let restOptions = {
    url: apiEndPoint,
    method: callType,
    timeout: timeoutParam || 20000, // timeout in ms
    headers:config.headers||null,
    raxConfig: {
        retry: retryCount || 0, // number of retry when facing 4xx or 5xx
        instance: instance,
        retryCondition: () => true,
        onRetryAttempt: err => {
            let tempError = Object.assign({}, err)//{...err}
            const cfg = rax.getConfig(err);
            delete tempError.config;
            delete tempError.request;
        },
        noResponseRetries: 3, // number of retry when facing connection error
        httpMethodsToRetry: ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT', 'POST', 'PATCH'],
        retryDelay: 3000,
        backoffType: 'static'
    }
};

var caller_id = caller||'';
if (dataPayLoad) restOptions = {...restOptions, data: dataPayLoad};

return new Promise((resolve, reject) => {

    instance(restOptions)
        .then(response => {
            logger.info(caller_id, '[API_CALL_SUCCESS] API call has succeeded');
            if (response) {
                const {status, data} = response;
                logger.info(caller_id, '[API_CALL_SUCCESS] API call Status Code: [' + status + ']');

                try {
                    if (status === 200 || status === 201) {
                        resolve(response);
                    } else {
                        reject(null);
                    }
                } catch (jsonParseError) {
                    reject(jsonParseError);
                }
            } else {
                resolve(null);
            }
        })
        .catch(error => {
            const {response, request, message, config} = error;
            reject(error);

});

I have overridden retryCondition I am not sure if its done in correct way. Can someone please let me know what wrong I am doing ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Got the fix . I over ride statusCodesToRetry property.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!