axios拦截器当status为200且数据为空时重试http请求

Kay*_*Kay 1 http node.js reactjs axios

我会短暂轮询端点,直到一些数据准备就绪为止,我想重试该请求最多 10 次。

当数据未准备好时,我收到带有空数组的 200。

当数据准备好时,我收到一个带有非空数组的 200 。

我使用以下库 https://github.com/axios/axios https://github.com/softonic/axios-retry

 try {

   const axiosInstance = axios.create();

   axiosInstance.interceptors.response.use((response) => {

     if (response.data.metrics.length === 0) {
       const error = new Error("Metrics Not Ready");
       return Promise.reject(error);

     }
     return response;
   }, (error) => {
     return Promise.reject(error);
   });

   axiosRetry(axiosInstance, {
     retries: 10,
     retryCondition: (error) => {
       console.log("RETRY CONDITION", error);
     },
   });

   const metrics = await axiosInstance(options);

 } catch (error) {
   console.log(error);
 }
Run Code Online (Sandbox Code Playgroud)

我创建了一个 axios 拦截器来检查数组的长度,如果它是 0 我会抛出错误。然而,这并没有被 axiosRetry 捕获,此时我想重试该请求。相反,它被捕获在 try catch 块中并结束。

sha*_*ncs 7

你不需要 axios-retry,你需要的只是 axios 及其响应拦截器,步骤如下:

  1. 检查数据是否为空。如果是,则抛出axios.Cancel错误。这将取消请求,调用错误处理程序而不是成功处理程序。
  2. 在错误处理程序中,如果没有超过最大重试次数(在您的情况下为 10),请重新发送 HTTP 请求。
  3. 继续运行步骤 1 和 2,直到获得数据或重试 10 次。

代码如下所示:

const axios = require('axios');

const MAX_RETRY = 10;
let currentRetry = 0;

function successHandler() {
  console.log('Data is Ready');
}

function errorHandler() {
  if (currentRetry < MAX_RETRY) {
    currentRetry++;
    console.log('Retrying...');
    sendWithRetry();
  } else {
    console.log('Retried several times but still failed');
  }
}

function sendWithRetry() {
  axios.get('http://example.com')
    .then(successHandler).catch(errorHandler);
}

axios.interceptors.response.use(function (response) {
  if (response.data.metrics.length) {
    throw new axios.Cancel('Operation canceled by the user.');
  } else {
    return response;
  }
}, function (error) {
  return Promise.reject(error);
});

sendWithRetry();
Run Code Online (Sandbox Code Playgroud)

对于 axios-retry,不幸的是,当响应状态为 时,您无法重试 HTTP 请求200 OK,因为 axios-retry 使用axios.interceptors.response.use但对“成功响应”不执行任何操作。

这是axios-retry的源文件中的相应代码es/index.js。可以看到成功响应的拦截器是null

export default function axiosRetry(axios, defaultOptions) {
  axios.interceptors.request.use(config => {
    const currentState = getCurrentState(config);
    currentState.lastRequestTime = Date.now();
    return config;
  });

  axios.interceptors.response.use(null, error => {
    const config = error.config;

    // If we have no information to retry the request
    if (!config) {
      return Promise.reject(error);
    }
    ....
Run Code Online (Sandbox Code Playgroud)