[英]UnhandledPromiseRejectionWarning, but I am handling errors?
I'm getting the following error:我收到以下错误:
Houston we got an err at getGames in index.js
(node:72197) UnhandledPromiseRejectionWarning: #<Object>
(node:72197) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:72197) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
error at getGames rp
Here is the relevant code:这是相关的代码:
Helper function to get Data from API (rp is request-promise-native)从 API 获取数据的辅助函数(rp 是 request-promise-native)
const getGames = async () => {
return await rp(`https://api.steampowered.com/IDOTA2Match_570/GetTopLiveGame/v1?key=${KEY}&partner=0`)
.catch(err => console.log('error at getGames rp'))
.error(err => console.log('actual error at getGames rp'))
}
I call this every 5 seconds我每 5 秒调用一次
app.get('/api/update', async (req, res) => {
const data = await getGames()
.catch(err => console.log('Houston we got an err at getGames in index.js'))
const dataJson = JSONbig.parse(data);
if (dataJson.game_list && dataJson.game_list.length) {
for (let i = 0; i < dataJson.game_list.length; i++) {
const game = dataJson.game_list[i];
...
The '/api/update' endpoint gets called every 5 seconds. '/api/update' 端点每 5 秒被调用一次。 I get the above error.
我收到上述错误。 My for loop stops iterating.
我的 for 循环停止迭代。 I'd like it to skip the one it's error'ing on, but it's saying im not handling the error.
我希望它跳过出错的那个,但它说我没有处理错误。 I have catch blocks and even error blocks and I can't seem to figure out why this is happening.
我有 catch 块甚至错误块,我似乎无法弄清楚为什么会发生这种情况。
I've also tried manually calling:我也试过手动调用:
`https://api.steampowered.com/IDOTA2Match_570/GetTopLiveGame/v1?key=${KEY}&partner=0`
in postman many times in quick succession, but postman never errors out.在邮递员中快速连续多次,但邮递员从不出错。 So the issue is in my code somewhere.
所以问题出在我的代码中。 I've handled all errors so I can't seem to find what i'm not handling.
我已经处理了所有错误,所以我似乎无法找到我没有处理的问题。
There are a few issues:有几个问题:
Only catch at a point where you can actually handle the error.仅在您可以实际处理错误的地方捕获。 Here, you probably only want to catch in the
.get
callback (otherwise, if you .catch
in getGames
, you'll return a resolved Promise, not a rejected Promise)在这里,您可能只想在
.get
回调中捕获(否则,如果您在getGames
.catch
,您将返回一个已解决的 Promise,而不是一个被拒绝的 Promise)
When an error occurs, don't try to parse the data (since it won't be populated) - trying to parse and iterate over it will result in more errors发生错误时,不要尝试解析数据(因为它不会被填充) - 尝试解析和迭代它会导致更多错误
There's no point in having an async
function that only has one await
that immediately gets returned拥有一个只有一个
await
立即返回的async
函数是没有意义的
const getGames = () => {
return rp(`https://api.steampowered.com/IDOTA2Match_570/GetTopLiveGame/v1?key=${KEY}&partner=0`);
};
app.get('/api/update', async (req, res) => {
const data = await getGames()
.catch(err => {
console.log('Houston we got an err at getGames in index.js');
// handle error
});
if (!data) {
// There was an error
return;
}
const dataJson = JSONbig.parse(data);
// rest of the code
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.