Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
katiegoines committed Mar 18, 2024
1 parent 8b69a08 commit ce5c996
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions tasks/console-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { launch } from 'puppeteer';
import { getSitemapUrls } from './get-sitemap-links';

const excludedErrors = [
{
type: 'Shortbread',
errorText:
"Shortbread failed to set user's cookie preference because the domain name that was passed"
}
];

const excludedScripts = [
'prod.assets.shortbread.aws',
'prod.tools.shortbread.aws',
'aa0.awsstatic.com'
];

const checkPage = async (url) => {
const errorsFound = [];
let browser = await launch({ headless: 'new' });

const page = await browser.newPage();

page
.on('pageerror', (message) => {
let errorText = message.message;
const excluded = excludedErrors.some((excludedError) => {
return errorText.includes(excludedError.errorText);
});

if (!excluded) {
errorsFound.push({
page: url,
message: errorText
});
}
})
.on('console', (message) => {
if (message.type().toLowerCase() === 'error') {
let errorText = message.text();
let callingScript = message.location().url;
const excludedFromError = excludedErrors.some((excludedError) => {
return errorText.includes(excludedError.errorText);
});
const excludedFromScript = excludedScripts.some((excludedScript) => {
return callingScript.includes(excludedScript);
});
const excluded = excludedFromError || excludedFromScript;

if (!excluded) {
errorsFound.push({
page: url,
message: errorText
});
}
}
});

await page.goto(url, { waitUntil: 'domcontentloaded' });

await browser.close();

return errorsFound;
};

const consoleErrors = async (domain) => {
let pagesToCheck = await getSitemapUrls(domain);
let errorMessage = '';
for (let i = 0; i < pagesToCheck.length; i++) {
let url = pagesToCheck[i];
console.log(`checking page ${url}`);
let errorsFound = await checkPage(url);
errorsFound.forEach((error) => {
errorMessage += `${error.message} found on ${error.page}\n`;
});
}
console.log(errorMessage);
return errorMessage;
};

export async function consoleErrors(domain = 'http://localhost:3000') {
return await consoleErrors(domain);
}

0 comments on commit ce5c996

Please sign in to comment.