0

Occasionally, when I run my puppeteer script, I’ll get a “page crashed” error. It’s most likely not a software error, but something to do with the proxy IPs I’m using/webpages I’m scraping. So all I’m really looking to do is just catch the error, close the current page, and open up a new page.

However, my try catch block doesn’t seem to be working. My code is structured like this:

try {
   //puppeteer code
}
catch(e) {
   console.log(‘caught error ‘ + e);
}

But when the page crashed error happens, I see “unhandled promise rejection error” instead of my console.log message. What’s going on here?

4
  • 1) you can use e.message to only print the message. 2) please provide more detailed code, do you have any code outside of try/catch block? 3) have you tried calling it on error event? e.g. page.on('error', msg => throw msg) Commented Dec 18, 2019 at 2:25
  • There’s no code outside of the try-catch block. I didn’t know that page.on(‘error’) was a thing...where exactly would I put that code? Commented Dec 18, 2019 at 3:54
  • put it somewhere after creating newPage() and before goto() Commented Dec 18, 2019 at 4:15
  • maybe catch errors where error might happen like await page.goto('example.com').catch(function (error) { throw new Error('this error happend'); }); Commented Dec 18, 2019 at 14:25

1 Answer 1

2

From here:

page.on('error', msg => {
  console.error(...);
  throw msg ;
});

And this is also advised to globally catch all uncaught errors in your code:

process.on('unhandledRejection', error => {
  console.error(...);
  throw error;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.