Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer can't catch failing request & errors

Tags:

puppeteer

I trying to collect data from failing requests and js error.

I'm using the following site: https://nitzani1.wixsite.com/marketing-automation/3rd-page

The site has a request to https://api.fixer.io/1latest, which returns a status code of 404,

also the page contains thw following js error:

"Uncaught (in promise) Fetch did not succeed"

I've tried to code bellow to catch the 404 and js error but couldn't. Not sure what I'm doing wrong, any idea as to how to solve it?

const puppeteer = require('puppeteer');

function wait (ms) {
    return new Promise(resolve => setTimeout(() => resolve(), ms));
}

var  run = async () => {
    const browser = await puppeteer.launch({
        headless: false,
        args: ['--start-fullscreen']
    });

    page = await browser.newPage();

    page.on('error', err=> {
        console.log('err: '+err);
    });

    page.on('pageerror', pageerr=> {
        console.log('pageerr: '+pageerr);
    });

    page.on('requestfailed', err => console.log('requestfailed: '+err));

    collectResponse = [];

    await page.on('requestfailed', rf => {
        console.log('rf: '+rf);
    });

    await page.on('response', response => {
        const url = response.url();
        response.buffer().then(
            b => {
                // console.log(url+' : '+response.status())
            },
            e => {
                console.log('response err');
            }
        );
    });
    await wait(500);

    await page.setViewport({ width: 1920, height: 1080 });
    await page.goto('https://nitzani1.wixsite.com/marketing-automation/3rd-page', {
    });
};

run();
like image 664
Captain_Meow_Meow Avatar asked May 25 '18 14:05

Captain_Meow_Meow


1 Answers

The complete worked answer is:

const puppeteer = require('puppeteer');

const run = async () => {
    const browser = await puppeteer.launch({
        headless: true
    });

    const page = await browser.newPage();
    // Catch all failed requests like 4xx..5xx status codes
    page.on('requestfailed', request => {
        console.log(`url: ${request.url()}, errText: ${request.failure().errorText}, method: ${request.method()}`)
    });
    // Catch console log errors
    page.on("pageerror", err => {
        console.log(`Page error: ${err.toString()}`);
    });
    // Catch all console messages
    page.on('console', msg => {
        console.log('Logger:', msg.type());
        console.log('Logger:', msg.text());
        console.log('Logger:', msg.location());

    });

    await page.setViewport({ width: 1920, height: 1080 });
    await page.goto('https://nitzani1.wixsite.com/marketing-automation/3rd-page', { waitUntil: 'domcontentloaded' });
    await page.waitFor(10000); // To be sure all exceptions logged and handled
    await browser.close();
};

run();

Save in .js file and easily run it.

like image 74
storenth Avatar answered Oct 13 '22 23:10

storenth