Can We Explicitly Catch Puppeteer (Chrome/Chromium) Error net::ERR_ABORTED?

Viewed 2704

Can we explicitly and specifically catch Puppeteer (Chromme/Chromium) error net::ERR_ABORTED? Or is string matching the only option currently?

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)

Ideally, this would be similar to a potential event we could catch with page.on('requestaborted')

1 Answers

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'})
  }
}

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

Related