不和谐.js:(节点:147)未处理的承诺拒绝警告:错误:超时超过 1000 毫秒



最近由于某种未知原因,我开始在控制台中看到常规错误。喜欢这个:

(node:147) UnhandledPromiseRejectionWarning: Error: timeout of 1000ms exceeded
at createError (/home/runner/ricebot/node_modules/axios/lib/core/createError.js:16:15)
at RedirectableRequest.handleRequestTimeout (/home/runner/ricebot/node_modules/axios/lib/adapters/http.js:280:16)
at RedirectableRequest.emit (events.js:314:20)
at RedirectableRequest.EventEmitter.emit (domain.js:483:12)
at Timeout._onTimeout (/home/runner/ricebot/node_modules/follow-redirects/index.js:166:12)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)

有时也会使机器人的延迟达到 30000 毫秒。

我的有趣命令中只有 Axios 部分,这是其中之一(但它仍然可以正常工作,只是记录错误):

const url = 'https://no-api-key.com/api/v2/animals/cat';
let image;
let fact;
try {
const { data } = await axios.get(url);
console.log(data);
image = data.image;
fact = data.fact;
} catch (e) {
console.log(e)
return message.channel.send('An error occured, please try again!');
}

这在过去不是一回事。

由于您的 axios 调用没有捕获,因此收到此错误或警告是正常的。

管理 axios 请求以防止此类问题非常重要。另外,我假设您显示的示例缺少一些数据,因为您不需要在 catch 状态之外定义结果变量,所以我假设您有一个循环或其他可能导致这种情况的东西。

如果您在循环中或使用 Promise.all 调用同一终端节点,有时您需要限制并发请求。如果不是这种情况,请忽略此部分。

首先,确保正确设置axios.default.timeout值,以定义如果有响应时何时取消请求。

定义响应状态 检查您的要求。

function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
}
const error: any = new Error(response.statusText);
error.response = response;
throw error;
}

定义解析代码以确保始终具有相同的结构。

function parseJSON(response) {
return response.data;
}

定义一个 catch 功能来检查错误并决定是引发错误还是只记录错误。

function catchError(e){
console.error(e);
// TODO send a message
return null;
}

让我们在一次带有 .catch 的调用中使用它们。

const myResult = await axios.get(url)
.then(checkStatus)
.then(parseJSON)
.catch(catchError);
console.log(myResult);

如果我没记错的话,你不能直接从承诺访问属性,因为从属性声明变量是同步的,而你的url()方法是异步的。
当响应未返回时,axios.get(url)的数据类型Promise,因此它没有您早期访问的data属性。因此,您必须等待响应。当返回响应时,该方法的数据类型将更改为其预期状态。那时您可以访问data属性 所以我认为你的代码应该如下所示:

const url = 'https://no-api-key.com/api/v2/animals/cat';
let image;
let fact;
try {
const axiosUrl = await axios.get(url);
const data = axiosUrl.data;
console.log(data);
image = data.image;
fact = data.fact;
} catch (e) {
console.log(e)
return message.channel.send('An error occured, please try again!');
}

假设此代码块位于异步上下文中,它应该可以正常工作。
我希望这个答案对你有用。

最新更新