尝试Catch无法捕获UnhandledPromiseRejectionWarning [英] Try Catch unable to catch UnhandledPromiseRejectionWarning

查看:156
本文介绍了尝试Catch无法捕获UnhandledPromiseRejectionWarning的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我有很好的机会找到我从木偶操作员那里获得的那些罕见的超时,但有些人如何暂停这些超时 - 我的问题是为什么?

I thought I had a pretty good catch to find those rare timeouts that I get from puppeteer, but some how this timeout is not caught by any of them - my question is why?

以下是代码:

var readHtml = (url) => {
    return new Promise( async (resolve,reject)=> {

        var browser = await puppeteer.launch()
        var page    = await browser.newPage()

        await page.waitForSelector('.allDataLoaded')

            .then(() => {
                console.log ("Finished reading: " + url)
                return resolve("COOL");
            })

            .catch((err) => {
                console.log ("Timeout or other error: ", err)
                return resolve("TRYAGAIN");
            });
})}

这是错误....

(node:23124) UnhandledPromiseRejectionWarning: Error: Navigation Timeout Exceeded: 30000ms exceeded at Promise.then 

(node:23124) 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)

我做了一些研究,说这可能是因为有些网址没有但是在puppeteer里面完成了newPage()

I did some research which said it might be because there are some urls not yet finished inside the puppeteer newPage()

但是为什么我的.catch怎么没有咳嗽?

But how come this does not get cough by my .catch?

我需要它来TRYAGAIN,以防它出于任何原因而失败。
现在它只是因错误而停止并且什么都不做。

I need it to "TRYAGAIN" in case it fails for what ever reason. Now it just stops with the error and does nothing.

推荐答案

你是正确的赶上 waitForSelector 及其链接的承诺,但你没有为启动 newPage 调用 - 它们之后没有连接到 catch

You're properly catching the waitForSelector and its chained promises, but you're not doing the same for the launch and newPage calls - they're not connected to the catch later.

因为异步函数已经自动返回Promises,你可以考虑完全避免使用Promise构造函数:

Because async functions automatically return Promises already, you might consider avoiding the Promise constructor entirely:

var readHtml = async (url) => {
  try {
    var browser = await puppeteer.launch()
    var page    = await browser.newPage()
  } catch(e) {
    // handle initialization error
  }

  await page.waitForSelector('.allDataLoaded')
    .then(() => {
    console.log ("Finished reading: " + url)
    return resolve("COOL");
  })
    .catch((err) => {
    console.log ("Timeout or other error: ", err)
    return resolve("TRYAGAIN");
  });
}

或者,你可以考虑把 catch 消费者中的code> readHtml

Or, you might consider putting the catch in the consumer of readHtml:

var readHtml = async (url) => {
  var browser = await puppeteer.launch()
  var page    = await browser.newPage()
  await page.waitForSelector('.allDataLoaded')
  console.log ("Finished reading: " + url)
};
readHtml(someurl)
  .catch((e) => console.log('err: ' + e));

这篇关于尝试Catch无法捕获UnhandledPromiseRejectionWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆