繁体   English   中英

axios-retry 不适用于状态为 4XX 的响应

[英]axios-retry is not working for response with status 4XX

我正在使用 axios 拨打 api 电话。 万一发生故障或响应状态。= 200 我需要重试 api 调用。 默认重试 axios 适用于 5XX 状态。 但是根据文档,我们可以根据我们的要求覆盖 retryCondition。

这是我的代码片段

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);

});

我已经覆盖了 retryCondition 我不确定它是否以正确的方式完成。 有人可以让我知道我做错了什么吗?

得到修复。 我超越了 statusCodesToRetry 属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM