我们可以显式地捕获Puppeteer(Chrome/Chromium)错误net::ERR_ABORTED吗?

xtupzzrd  于 2023-04-27  发布在  Go
关注(0)|答案(2)|浏览(294)

我们可以明确地捕获Puppeteer(Chromme/Chromium)错误net::ERR_ABORTED吗?或者字符串匹配当前唯一的选项?

page.goto(oneClickAuthPage).catch(e => {
  if (e.message.includes('net::ERR_ABORTED')) {}
})

/*  "net::ERROR_ABORTED" occurs for sub-resources on a page if we navigate
 *  away too quickly. I'm specifically awaiting a 302 response for successful
 *  login and then immediately navigating to the auth-protected page.
 */
await page.waitForResponse(res => res.url() === href && res.status() === 302)
page.goto(originalRequestPage)

理想情况下,这将类似于我们可以用page.on('requestaborted')捕获的潜在事件

xxls0lw8

xxls0lw81#

其他

如果您遇到错误:net::ERR_ABORTED at https:....
然后主要是由于互联网问题,快速导航或网站问题
试试下面给出的代码--

var count=0;
  const page = await browser.newPage()
        page.setDefaultNavigationTimeout(0)
      function loop(){
        page.goto('Your URL').catch(err => {
    // If any Error occurs then run the goto 3 or 4 times which worked for me
      if(err && count<4){
        console.log(count) 
          count++
          if(count>3){
          console.log("Internet/Website Problem")
        page.close();  
        }
    // Use Selector to wait for your element or thing to appear
         page.waitForSelector("Your Element" , {visible: true, timeout: ((count*1000)+1000)}).catch(()=>{loop()})
        } 

  })
  }
await loop()

..
.(您的进一步代码)

对于给定问题

如果你想捕获Puppeteer(Chromme/Chromium)错误,比如net::ERR_ABORTED?
然后你必须使用字符串格式的错误,根据我的研究,到现在为止,我没有找到任何错误状态代码。

3gtaxfhh

3gtaxfhh2#

我建议把你的API调用放在一个trycatch块中,如果失败了,你就可以捕获错误,就像你现在做的一样,但是这样看起来更好

try {
 await page.goto(PAGE)
} catch(error) {
  console.log(error) or console.error(error)
  //do specific functionality based on error codes
  if(error.status === 300) {
    //I don't know what app you are building this in
    //But if it's in React, here you could do 
    //setState to display error messages and so forth
    setError('Action aborted')
    
    //if it's in an express app, you can respond with your own data
    res.send({error: 'Action aborted'})
  }
}

如果Puppeteer中止时的错误响应中没有特定的错误代码,则意味着Puppeteer的API没有被编码为返回这样的数据,不幸的是:
像你在问题中所做的那样做错误消息检查并不少见。不幸的是,这是我们唯一能做的,因为这是我们所要做的:

相关问题