简体   繁体   English

UnhandledPromiseRejectionWarning,但我正在处理错误?

[英]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.

相关问题 我是否使用async / await函数和Mongoose处理错误? - Am I handling errors the good way with async/await functions and Mongoose? 如何修复 UnhandledPromiseRejectionWarning:错误:读取 ETIMEDOUT 和 UnhandledPromiseRejectionWarning:错误:写入 EPROTO 错误 - How do I fix UnhandledPromiseRejectionWarning: Error: read ETIMEDOUT and UnhandledPromiseRejectionWarning: Error: write EPROTO errors 为什么在运行我的代码时会收到错误“UnhandledPromiseRejectionWarning”? - Why am I getting error "UnhandledPromiseRejectionWarning" when running my code? JS Promises-为什么我会看到UnhandledPromiseRejectionWarning和DeprecationWarning? - JS Promises - Why am I seeing the UnhandledPromiseRejectionWarning and DeprecationWarning? 我正在构建一个软件包,该如何处理对客户的承诺产生的错误处理? - In a package I am building that, how can I leave handling of errors created by promises to consumers of the package? UnhandledPromiseRejectionWarning:未处理的承诺拒绝错误,我正在使用 Node JS 和 mongo Db - UnhandledPromiseRejectionWarning: Unhandled promise rejection error I am getting I am using Node JS and mongo Db 我是否正确处理了这些承诺? - Am I handling these promises correctly? 为什么在我的无服务器项目上运行 npm 构建时出现此 UnhandledPromiseRejectionWarning 错误? - Why am I getting this UnhandledPromiseRejectionWarning error when running npm build on my serverless project? 我收到2美元的未定义错误 - I am getting 2 $ in not defined errors 为什么我收到错误“UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined”? - Why am I getting the error “UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM