From df9bd461a6182346ce929c535c1cd9b17608608e Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sun, 27 Sep 2020 13:58:36 +0200 Subject: [PATCH] Display non-chained errors more simply, without a summary section --- src/packages/render/index.js | 41 ++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/packages/render/index.js b/src/packages/render/index.js index e35aed2..b89d787 100644 --- a/src/packages/render/index.js +++ b/src/packages/render/index.js @@ -19,7 +19,24 @@ function formattedErrorHeading(error, colorAll = false) { let coloredMessage = (colorAll === true) ? chalk.red(formattedMessage) : formattedMessage; return `${formattedName} ${coloredMessage}`; - // return chalk.red(`${chalk.bold(error.name)}: ${formattedMessage}`); +} + +function formattedError(error, indentation = "", prefix = "") { + let strippedStack = stripErrorFromStack(error.stack); + + let formattedStack = strippedStack.split("\n").map((line) => { + if (line.trim().length === 0) { + return null; + } else if (line[0] === " ") { + return indentation + line; + } else { + return indentation + ` ${line}`; + } + }).filter(line => line != null).join("\n"); + + let heading = indentation + prefix + formattedErrorHeading(error); + + return `${heading}\n${formattedStack}`; } module.exports = function renderError(_error, _options) { @@ -34,6 +51,11 @@ module.exports = function renderError(_error, _options) { let {allStacktraces} = options; let errors = getChain(error); + + if (errors.length === 1) { + // Special case: there's not actually a cause chain, so we should render the error more simply + return formattedError(errors[0]); + } else { let detailedErrorsToDisplay = (allStacktraces === true) ? errors : errors.slice(-1); let summary = errors.map((error, i) => { @@ -57,21 +79,7 @@ module.exports = function renderError(_error, _options) { let causedByPrefix = (i > 0 ? "Caused by: " : ""); let causedByPadding = (i > 0) ? " " : ""; - let strippedStack = stripErrorFromStack(error.stack); - - let formattedStack = strippedStack.split("\n").map((line) => { - if (line.trim().length === 0) { - return null; - } else if (line[0] === " ") { - return causedByPadding + line; - } else { - return causedByPadding + ` ${line}`; - } - }).filter(line => line != null).join("\n"); - - let heading = causedByPadding + causedByPrefix + formattedErrorHeading(error); - - return `${heading}\n${formattedStack}`; + return formattedError(error, causedByPadding, causedByPrefix); }).join("\n\n"); let stacktraceSection = (allStacktraces === true) @@ -79,4 +87,5 @@ module.exports = function renderError(_error, _options) { : `${chalk.cyan("Stacktrace for original error:")}\n\n${stacktraces}`; return `${summary}\n\n${stacktraceSection}`; + } };