Skip to content

Commit

Permalink
Better prerender crash message when error originates in node_modules/
Browse files Browse the repository at this point in the history
  • Loading branch information
VanTanev committed Sep 15, 2020
1 parent 172e9ec commit d3c68fd
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions packages/cli/lib/lib/webpack/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const stackTrace = require('stack-trace');
const URL = require('url');
const { SourceMapConsumer } = require('source-map');

module.exports = function(env, params) {
module.exports = function (env, params) {
params = params || {};

let entry = resolve(env.dest, './ssr-build/ssr-bundle.js');
Expand Down Expand Up @@ -33,7 +33,9 @@ module.exports = function(env, params) {
));
return renderToString(preact.h(app, { ...params, url }));
} catch (err) {
let stack = stackTrace.parse(err).filter(s => s.getFileName() === entry)[0];
let stack = stackTrace
.parse(err)
.filter((s) => s.getFileName() === entry)[0];
if (!stack) {
throw err;
}
Expand All @@ -55,13 +57,13 @@ async function handlePrerenderError(err, env, stack, entry) {
}

if (sourceMapContent) {
await SourceMapConsumer.with(sourceMapContent, null, consumer => {
await SourceMapConsumer.with(sourceMapContent, null, (consumer) => {
position = consumer.originalPositionFor({
line: stack.getLineNumber(),
column: stack.getColumnNumber(),
});
});

if (position.source) {
position.source = position.source
.replace('webpack://', '.')
Expand Down Expand Up @@ -101,17 +103,29 @@ async function handlePrerenderError(err, env, stack, entry) {

process.stderr.write('\n');
process.stderr.write(red(`${errorMessage}\n`));
process.stderr.write(`method: ${methodName}\n`);
if (sourceMapContent) {
// check if we have methodName (ie, the error originated in user code)
if (methodName) {
process.stderr.write(`method: ${methodName}\n`);
if (sourceMapContent & sourceCodeHighlight) {
process.stderr.write(
`at: ${sourcePath}:${position.line}:${position.column}\n`
);
process.stderr.write('\n');
process.stderr.write('Source code:\n\n');
process.stderr.write(sourceCodeHighlight);
process.stderr.write('\n');
} else {
process.stderr.write('\n');
process.stderr.write('Stack:\n\n');
process.stderr.write(JSON.stringify(stack, null, 4) + '\n');
}
} else {
process.stderr.write(
`at: ${sourcePath}:${position.line}:${position.column}\n`
yellow(
'Cannot determine error position. This most likely means it originated in node_modules.'
)
);
process.stderr.write('\n');
process.stderr.write('Source code:\n\n');
process.stderr.write(sourceCodeHighlight);
process.stderr.write('\n');
} else {
process.stderr.write(stack.toString() + '\n');
process.stderr.write('\n\n');
}
process.stderr.write(
`This ${
Expand Down

0 comments on commit d3c68fd

Please sign in to comment.