[axios]如何使用 axios 捕获状态 = 已取消?

2024-05-15 154 views
3

我断开了数据库并向 api 发送了请求。在 Chrome 网络选项卡中,状态从待处理更改为已取消,但没有发现任何错误。

我缺少什么机制来捕捉状态=已取消吗?

回答

6

您能否提供发出请求并处理错误的代码?

0

我也有同样的问题,没找到相关的讨论。

7

您能否在浏览器中运行以下代码并提供输出?

axios.post(...).then(response => {
  console.log('Success', response);
}).catch(error => {
  console.log('Error', error);
});
8

我没有完全测试 @srahulprdxn 发布的内容。但我也有同样的问题。

要重现您可以尝试以下操作:

设置超时例如300

axios.defaults.timeout = 300;

该请求将被“取消”

屏幕截图 2016-04-25 10 27 47 pm

我们应该能够捕获“catch”上的错误。

axios.get(...).catch(error => {
  console.log('Get canceled error', error);
});
9

请参阅问题#226。它已在 0.10.0 中修复。您使用的是最新版本的 axios 吗?

7

酷,感谢您的回复@nickuraltsev

2

@nickuraltsev 我将发布结果

axios.post(...).then(response => {
  console.log('Success', response);
}).catch(error => {
  console.log('Error', error);
});

很快。

5

由于不活动而关闭此功能。如果需要,请随时重新开放。

0

@nickuraltsev 我在 React Native 0.24.1 中使用版本 0.13.0,但它不起作用,catch 不会在超时时触发,它会永远消失

4

@nickuraltsev,我也看到了同样的问题。我无法让 axios 赶上超时。

我的代码:

axios.get(<some api endpoint>, {timeout: 10}).then(() => { debugger; }).catch(() => { debugger; }).finally(() => { debugger; })

在此示例中,网络选项卡显示请求已取消,但没有触发任何处理程序。

7

...aaand,从 0.9 版本更新到最新版本的 axios 为我解决了这个问题:)

4

我在里面看到了这个,0.16.0它回归了吗?

8

我在 0.16.0 中看到了这个,它有退化吗?

只需使用一个返回 204 的小型 python 服务器来测试它是否按预期工作,因此请忽略我之前的评论。

5

我找到这里是因为我遇到了无声超时,所以对于过去的自己,这里有一个提示:

axios请求的默认超时时间可能比父进程的超时时间长,这会吞掉这个错误。手动将 Axios 请求超时设置得很短,您会看到错误,例如

axios.get(url, { timeout: 5000 })
2

我在 中看到了这一点17.1,有人可以解释一下 的状态吗cancelled

2

@nickuraltsev,我也看到了同样的问题。我无法让 axios 赶上超时。

我的代码:

axios.get(<some api endpoint>, {timeout: 10}).then(() => { debugger; }).catch(() => { debugger; }).finally(() => { debugger; })

在此示例中,网络选项卡显示请求已取消,但没有触发任何处理程序。

我也有这个问题。解决了吗?

0

我为此编写的包装器 https://www.npmjs.com/package/@nelsonomuto/axios-request-timeout

https://github.com/nelsonomuto/axios-request-timeout

import axiosRequest from '@nelsonomuto/axios-request-timeout';

try {
  await axiosRequest({
    url: 'https://www.cnn.com/',
    method: 'GET',
    timeout: 10
  });
} catch(error) {
  console.log({ error });
  expect(error).toEqual({'message': 'timeout cancel'});
}

通过测试


test('request', async () => {
  let resp = await axiosRequest({
    url: 'https://www.cnn.com/',
    method: 'GET'
  });
  expect(resp.status).toEqual(200);

});
test('cancel request', async () => {
  try {
    await axiosRequest({
      url: 'https://www.cnn.com/',
      method: 'GET',
      timeout: 10
    });
  } catch(error) {
    console.log({ error });
    expect(error).toEqual({'message': 'timeout cancel'});
  }
});
5

检查ECONNABORTED错误代码对我有用:

axios.post(...).then(response => {
  console.log('Success', response);
}).catch(error => {
  if (e.code === 'ECONNABORTED') {
    // the request has either been canceled due to a network timeout,
    // or because the device is offline
  }
});
6

使用取消标记时,您可以使用以下axios.isCancel(error)函数进行检查:

const source = axios.CancelToken.source();

try {
  setTimeout(() => {
    source.cancel('Cancelling after 10ms');
  }, 10);
  await axios.post('http://localhost:8080/foo', { foo: bar }, { cancelToken: source.token });
}
catch (error) {
  if (axios.isCancel(error)) {
    console.log('Request cancelled...');
  }
}

我不知道这是否适用于从其他地方取消的请求,但如果您在谷歌上搜索令牌取消的答案,这可能会对您有所帮助。