Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check console errors #7060

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
/src/themes @aws-amplify/documentation-team
/src/utils @aws-amplify/documentation-team
/tasks @aws-amplify/documentation-team
.github @aws-amplify/documentation-team

#Protected Content
/src/protected @reesscot @srquinn21 @Milan-Shah @swaminator
39 changes: 39 additions & 0 deletions .github/workflows/check_for_console_errors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CheckConsoleErrors
on:
pull_request:
branches: [main]
types: [opened, synchronize]
permissions:
contents: read
id-token: write
jobs:
CheckConsoleErrors:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 https://github.com/actions/checkout/commit/f43a0e5ff2bd294095638e18286ca9a3d1956744
- name: Setup Node.js 20.x
uses: actions/setup-node@e33196f7422957bea03ed53f6fbb155025ffc7b8 # v3.7.0 https://github.com/actions/setup-node/commit/e33196f7422957bea03ed53f6fbb155025ffc7b8
with:
node-version: 20.x
- name: Install Dependencies
run: yarn
- name: Run Build
run: yarn build
env:
NODE_OPTIONS: --max_old_space_size=4096
- name: Run Server
run: |
node ./node_modules/.bin/serve client/www/next-build --no-request-logging &
sleep 5
- name: Run Console Errors
id: consoleErrors
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 https://github.com/actions/github-script/commit/60a0d83039c74a4aee543508d2ffcb1c3799cdea
with:
result-encoding: string
script: |
const { consoleErrors } = require('./tasks/console-errors.js');
return await consoleErrors();
- name: Fail if console errors have been found
if: ${{ steps.consoleErrors.outputs.result }}
run: exit 1
89 changes: 89 additions & 0 deletions tasks/console-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const puppeteer = require('puppeteer'); // eslint-disable-line
const { getSitemapUrls } = require('./get-sitemap-links'); // eslint-disable-line

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 puppeteer.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++) {
try {
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`;
});
} catch (error) {
console.log(error);
}
}
console.log(errorMessage);
return errorMessage;
};

module.exports = {
consoleErrors: async (domain = 'http://localhost:3000') => {
return await consoleErrors(domain);
}
};
42 changes: 42 additions & 0 deletions tasks/get-sitemap-links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const puppeteer = require('puppeteer'); // eslint-disable-line

const DOMAIN = 'https://docs.amplify.aws';
const SITEMAP_URL = 'https://docs.amplify.aws/sitemap.xml';

const getSitemapUrls = async (localDomain) => {
let browser = await puppeteer.launch({ headless: 'new' });

const page = await browser.newPage();

let siteMap = localDomain ? `${localDomain}/sitemap.xml` : SITEMAP_URL;
let response = await page.goto(siteMap);

const siteMapUrls = [];

if (response && response.status() && response.status() === 200) {
const urlTags = await page.evaluateHandle(() => {
return document.getElementsByTagName('loc');
});
const numOfLinks = await page.evaluate((e) => e.length, urlTags);
for (let i = 0; i < numOfLinks; i++) {
let url = await page.evaluate(
(urlTags, i) => urlTags[i].innerHTML,
urlTags,
i
);
if (localDomain) {
// Currently the sitemap is always generated with the prod docs domain so we need to replace this with localhost
url = url.replace(DOMAIN, localDomain);
}
siteMapUrls.push(url);
}
}
browser.close();
return siteMapUrls;
};

module.exports = {
getSitemapUrls: async (domain) => {
return await getSitemapUrls(domain);
}
};
Loading