Skip to content

Commit

Permalink
Tag errors thrown during prerendering
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Sep 6, 2021
1 parent 1d9791f commit bdc4a54
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
21 changes: 14 additions & 7 deletions packages/wmr/src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ export default async function build(options) {

if (!options.prerender) return;

const { routes } = await prerender(options);
const routeMap = routes.reduce((s, r) => {
s += `\n ${r.url}`;
if (r._discoveredBy) s += kl.dim(` [from ${r._discoveredBy.url}]`);
return s;
}, '');
process.stdout.write(kl.bold(`Prerendered ${routes.length} page${routes.length == 1 ? '' : 's'}:`) + routeMap + '\n');
try {
const { routes } = await prerender(options);
const routeMap = routes.reduce((s, r) => {
s += `\n ${r.url}`;
if (r._discoveredBy) s += kl.dim(` [from ${r._discoveredBy.url}]`);
return s;
}, '');
process.stdout.write(
kl.bold(`Prerendered ${routes.length} page${routes.length == 1 ? '' : 's'}:`) + routeMap + '\n'
);
} catch (err) {
err.hint = 'The following error was thrown during prerendering:';
throw err;
}
}
6 changes: 2 additions & 4 deletions packages/wmr/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ function run(p) {
* @param {Error} err
*/
async function catchException(err) {
// TODO: Unhandled exceptions originating in workers will
// end up here too. Find a way to tag prerender errors somehow.

let text = '';
let stack = '';
let codeFrame = '';
Expand Down Expand Up @@ -108,7 +105,8 @@ async function catchException(err) {
const printFrame = codeFrame ? codeFrame + '\n' : '';
const printStack = stack ? kl.dim(stack + '\n\n') : '';

process.stderr.write(`\n${kl.red(text)}${printFrame || '\n'}${printStack}`);
const hint = err.hint ? err.hint + '\n\n' : '';
process.stderr.write(`\n${kl.cyan(hint)}${kl.red(text)}${printFrame || '\n'}${printStack}`);
process.exit(1);
}

Expand Down
10 changes: 10 additions & 0 deletions packages/wmr/test/fixtures/prerender-error/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8" />
<title>default title</title>
</head>
<body>
<script src="./index.js" type="module"></script>
</body>
</html>
3 changes: 3 additions & 0 deletions packages/wmr/test/fixtures/prerender-error/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function prerender() {
return window.foo.bar;
}
11 changes: 11 additions & 0 deletions packages/wmr/test/production.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,17 @@ describe('production', () => {
expect(instance.output.join('\n')).toMatch(/No prerender\(\) function/i);
});
});

it('should catch uncaught exceptions during prerendering', async () => {
await loadFixture('prerender-error', env);
instance = await runWmr(env.tmp.path, 'build', '--prerender');
const code = await instance.done;

await withLog(instance.output, async () => {
expect(code).toBe(1);
expect(instance.output.join('\n')).toMatch(/The following error was thrown during prerendering/i);
});
});
});

describe('Code Splitting', () => {
Expand Down

0 comments on commit bdc4a54

Please sign in to comment.