Skip to content

Commit 8a6c139

Browse files
Jami LurockJami Lurock
authored andcommitted
add error handling and additional error checking for console error messages
1 parent b8f1f1c commit 8a6c139

File tree

1 file changed

+57
-45
lines changed

1 file changed

+57
-45
lines changed

projects/cli/src/check-errors/verify-sandboxes.ts

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SANDBOX_MENU_ITEMS_FILE, SandboxFileInformation } from '../build-sandbo
44
import { ErrorReporter, REPORT_TYPE } from '../error-reporter';
55
import { Config } from '../configure';
66
import { waitForNgServe } from '../utils';
7+
78
const chalk = require('chalk');
89

910
// Used to tailor the version of headless chromium ran by puppeteer
@@ -24,7 +25,9 @@ let reporter: ErrorReporter;
2425

2526
// Ensure Chromium instances are destroyed on error
2627
process.on('unhandledRejection', async () => {
27-
if (browser) { await browser.close(); }
28+
if (browser) {
29+
await browser.close();
30+
}
2831
});
2932

3033
export async function verifySandboxes(config: Config) {
@@ -68,7 +71,8 @@ async function main(config: Config) {
6871
async function setupPageAndErrorHandling(hostUrl): Promise<Page> {
6972
const page = await browser.newPage();
7073
page.on('console', (msg: ConsoleMessage) => onConsoleErr(msg));
71-
await page.goto(hostUrl);
74+
console.log(`Navigating to ${hostUrl}`);
75+
await page.goto(hostUrl, {waitUntil: 'load', timeout: 60000});
7276
return page;
7377
}
7478

@@ -94,40 +98,43 @@ async function openScenario(scenario: ScenarioSummary, page: Page) {
9498
*/
9599
function getSandboxMetadata(config: Config, baseUrl: string, selectRandomScenario: boolean): ScenarioSummary[] {
96100
const scenarios: ScenarioSummary[] = [];
97-
98-
loadSandboxMenuItems().forEach((sandboxItem: SandboxFileInformation) => {
99-
if (!config.pathToSandboxes || config.pathToSandboxes.some(vp => sandboxItem.key.includes(vp))) {
100-
if (selectRandomScenario) {
101-
const randomItemKey = getRandomKey(sandboxItem.scenarioMenuItems.length);
102-
for (const item of sandboxItem.scenarioMenuItems) {
103-
if (item.key === randomItemKey) {
104-
scenarios.push(
105-
{
106-
name: sandboxItem.key,
107-
description: item.description,
108-
sandboxKey: sandboxItem.key,
109-
scenarioKey: item.key
110-
}
111-
);
112-
break;
101+
try {
102+
loadSandboxMenuItems().forEach((sandboxItem: SandboxFileInformation) => {
103+
if (!config.pathToSandboxes || config.pathToSandboxes.some(vp => sandboxItem.key.includes(vp))) {
104+
if (selectRandomScenario) {
105+
const randomItemKey = getRandomKey(sandboxItem.scenarioMenuItems.length);
106+
for (const item of sandboxItem.scenarioMenuItems) {
107+
if (item.key === randomItemKey) {
108+
scenarios.push(
109+
{
110+
name: sandboxItem.key,
111+
description: item.description,
112+
sandboxKey: sandboxItem.key,
113+
scenarioKey: item.key
114+
}
115+
);
116+
break;
117+
}
113118
}
119+
} else {
120+
// Grab all scenarios
121+
sandboxItem.scenarioMenuItems
122+
.forEach((item) => {
123+
scenarios.push(
124+
{
125+
name: sandboxItem.key,
126+
description: item.description,
127+
sandboxKey: sandboxItem.key,
128+
scenarioKey: item.key
129+
}
130+
);
131+
});
114132
}
115-
} else {
116-
// Grab all scenarios
117-
sandboxItem.scenarioMenuItems
118-
.forEach((item) => {
119-
scenarios.push(
120-
{
121-
name: sandboxItem.key,
122-
description: item.description,
123-
sandboxKey: sandboxItem.key,
124-
scenarioKey: item.key
125-
}
126-
);
127-
});
128133
}
129-
}
130-
});
134+
});
135+
} catch (err) {
136+
throw new Error(`Error getting sandbox metadata: ${err}`);
137+
}
131138

132139
return scenarios;
133140
}
@@ -147,19 +154,24 @@ function loadSandboxMenuItems(): SandboxFileInformation[] {
147154
* Callback when Chromium page encounters a console error
148155
*/
149156
function onConsoleErr(msg: ConsoleMessage) {
150-
if (msg.type() === 'error') {
151-
console.error(chalk.red(`Error in ${currentScenario} (${currentScenarioDescription}):`));
152-
const getErrors = (type: string, getValue: (_: any) => string) => msg.args()
153-
.map(a => (a as any)._remoteObject)
154-
.filter(o => o.type === type)
155-
.map(getValue);
156-
const stackTrace = getErrors('object', o => o.description);
157-
const errorMessage = getErrors('string', o => o.value);
158-
const description = stackTrace.length ? stackTrace : errorMessage;
159-
description.map(d => console.error(d));
160-
if (description.length) {
161-
reporter.addError(description, currentScenario, currentScenarioDescription);
157+
try {
158+
if (msg.type() === 'error') {
159+
console.error(chalk.red(`Error in ${currentScenario} (${currentScenarioDescription}):`));
160+
const getErrors = (type: string, getValue: (_: any) => string) => msg.args()
161+
.map(a => (a as any)._remoteObject)
162+
.filter(o => o.type === type)
163+
.map(getValue);
164+
const stackTrace = getErrors('object', o => o.description);
165+
const errorMessage = getErrors('string', o => o.value);
166+
let description = stackTrace.length ? stackTrace : errorMessage;
167+
description = description.length ? description : [msg.text()];
168+
description.map(d => console.error(d));
169+
if (description.length) {
170+
reporter.addError(description, currentScenario, currentScenarioDescription);
171+
}
162172
}
173+
} catch (err) {
174+
console.error(`Error handling console errors: ${err}`);
163175
}
164176
}
165177

0 commit comments

Comments
 (0)