我们可以明确捕获 Puppeteer (Chrome/Chromium) 错误 net::ERR_ABORTED 吗?

Ant*_*ris 13 javascript google-chrome chromium puppeteer

我们可以明确且具体地捕获 Puppeteer (Chrome/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)
Run Code Online (Sandbox Code Playgroud)

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

小智 -1

I'd recommend putting your api calls and so in a trycatch block If it fails, you catch the error, like you are currently doing. But it just looks a bit nicer

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'})
  }
}
Run Code Online (Sandbox Code Playgroud)

If there are not specific error codes in the error responses for when Puppeteer is aborted, it means that Puppeteer's API has not been coded to return data like that, unfortunately :')

It's not too uncommon to do error messages checks like you are doing in your question. It's, unfortunately, the only way we can do it, since this is what we're given to work with :'P