11

Can we explicitly and specifically catch Puppeteer (Chrome/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')

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
anthumchris
  • 8,245
  • 2
  • 28
  • 53

3 Answers3

0

For Others

If you are having problem of Error: net::ERR_ABORTED at https:....
Then it's mainly due Internet problem, Quick navigation or Website problems
Try the code given below:

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()

For Given Question

If you want to catch Puppeteer (Chromme/Chromium) errors like net::ERR_ABORTED?
Then you have to use Errors in string format and according to my research till now I don't find any Error status codes.

Aaron Meese
  • 1,670
  • 3
  • 22
  • 32
Logic
  • 21
  • 6
0

Since nobody else directly answered your question, string matching still appears to be the only option.

Here's some example code that I use on areas that are particularly prone to the ERR_ABORTED error:

try {
    await page.goto(oneClickAuthPage)
} catch (err) {
    if (err instanceof Error && err.message.startsWith('net::ERR_ABORTED')) {
        console.log("Caught ERR_ABORTED")
        // handle the error however you want
    }
    else {
        throw err
    }
}

Here's where I originally got this solution, minus the parts that I edited to match your question.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
-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'})
  }
}

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

Ruben Verster
  • 301
  • 1
  • 8