2021-09-17 23:04:09 +02:00
|
|
|
async function listenForErrors(browser, page) {
|
2021-11-01 08:40:39 +01:00
|
|
|
const ignoredErrors = ['ERR_ABORTED', 'MEDIA_ERR_SRC_NOT_SUPPORTED', '404'];
|
2021-09-17 23:04:09 +02:00
|
|
|
|
|
|
|
// Emitted when the page emits an error event (for example, the page crashes)
|
|
|
|
page.on('error', (error) => {
|
|
|
|
throw new Error(`❌ ${error}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
browser.on('error', (error) => {
|
|
|
|
throw new Error(`❌ ${error}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Emitted when a script within the page has uncaught exception
|
|
|
|
page.on('pageerror', (error) => {
|
|
|
|
throw new Error(`❌ ${error}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Catch all failed requests like 4xx..5xx status codes
|
|
|
|
page.on('requestfailed', (request) => {
|
2021-11-01 08:40:39 +01:00
|
|
|
const ignoreError = ignoredErrors.some((e) =>
|
|
|
|
request.failure().errorText.includes(e)
|
|
|
|
);
|
2021-09-17 23:04:09 +02:00
|
|
|
if (!ignoreError) {
|
|
|
|
throw new Error(
|
|
|
|
`❌ url: ${request.url()}, errText: ${
|
|
|
|
request.failure().errorText
|
|
|
|
}, method: ${request.method()}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Listen for console errors in the browser.
|
2021-11-01 08:40:39 +01:00
|
|
|
page.on('console', (msg) => {
|
2021-09-17 23:04:09 +02:00
|
|
|
const type = msg._type;
|
|
|
|
if (type !== 'error') {
|
2021-11-01 08:40:39 +01:00
|
|
|
return;
|
2021-09-17 23:04:09 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:40:39 +01:00
|
|
|
const ignoreError = ignoredErrors.some((e) => msg._text.includes(e));
|
2021-09-17 23:04:09 +02:00
|
|
|
if (!ignoreError) {
|
|
|
|
throw new Error(`❌ ${msg._text}`);
|
|
|
|
}
|
2021-11-01 08:40:39 +01:00
|
|
|
});
|
2021-09-17 23:04:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.listenForErrors = listenForErrors;
|