Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome puppeteer Close page on error event

Tags:

I want to close pages when puppeteer faces on any error , sometimes page the page that i try to load crashes and it doesnt call .close();

(async () => {
const page = await browser.newPage();
await page.setViewport({width: resWidth, height: resHeight});
await page.goto(d["entities"]["urls"][0]["expanded_url"], {timeout :90000});
await page.screenshot({path: './resimdata/'+d['id']+'.png' ,fullPage: true});
await page.close();
})();
like image 587
Mustafa Marsli Topaloğlu Avatar asked Oct 10 '17 18:10

Mustafa Marsli Topaloğlu


1 Answers

There is an issue/PR on puppeteer repo regarding this which will be helpful in similar situation.

Related Issue link: https://github.com/GoogleChrome/puppeteer/issues/952

Meanwhile, you can try this little hack, if the PR is there on version 0.12+, we don't have to worry about the following code.

(async() => {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();

    function handleClose(msg){
        console.log(msg);
        page.close();
        browser.close();
        process.exit(1);
    }

    process.on("uncaughtException", () => {
        handleClose(`I crashed`);
    });

    process.on("unhandledRejection", () => {
        handleClose(`I was rejected`);
    });

    await page.goto("chrome://crash");

})();

Which will output something like the following,

▶ node app/app.js
I was rejected
like image 135
Md. Abu Taher Avatar answered Oct 13 '22 01:10

Md. Abu Taher