|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const chalk = require("chalk");
|
|
|
|
|
const syncpipe = require("syncpipe");
|
|
|
|
|
|
|
|
|
|
const { validateArguments } = require("@validatem/core");
|
|
|
|
|
const required = require("@validatem/required");
|
|
|
|
@ -12,14 +13,50 @@ const stripErrorFromStack = require("./strip-error-from-stack");
|
|
|
|
|
const isInstanceOf = require("./is-instance-of");
|
|
|
|
|
const getChain = require("../get-chain");
|
|
|
|
|
|
|
|
|
|
function formattedErrorHeading(error, colorAll = false) {
|
|
|
|
|
let formattedMessage = error.message.trim().replace(/\n/g, " \\n ");
|
|
|
|
|
function prefixExtraLines(string, prefix) {
|
|
|
|
|
return string
|
|
|
|
|
.split("\n")
|
|
|
|
|
.map((line, i) => {
|
|
|
|
|
if (i > 0) {
|
|
|
|
|
return prefix + line;
|
|
|
|
|
} else {
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.join("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formattedErrorHeading(error, colorAll = false, indentation) {
|
|
|
|
|
let formattedMessage = syncpipe(error.message, [
|
|
|
|
|
(_) => _.trim(),
|
|
|
|
|
(_) => (indentation != null)
|
|
|
|
|
? prefixExtraLines(_, indentation)
|
|
|
|
|
: _
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
let formattedName = chalk.red(`${chalk.bold(error.name)}:`);
|
|
|
|
|
|
|
|
|
|
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 +71,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) => {
|
|
|
|
@ -50,28 +92,14 @@ module.exports = function renderError(_error, _options) {
|
|
|
|
|
|
|
|
|
|
// console.log(cleanStack);
|
|
|
|
|
|
|
|
|
|
return prefix + formattedErrorHeading(error);
|
|
|
|
|
return prefix + formattedErrorHeading(error, false, " ");
|
|
|
|
|
}).join("\n");
|
|
|
|
|
|
|
|
|
|
let stacktraces = detailedErrorsToDisplay.map((error, i) => {
|
|
|
|
|
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 +107,5 @@ module.exports = function renderError(_error, _options) {
|
|
|
|
|
: `${chalk.cyan("Stacktrace for original error:")}\n\n${stacktraces}`;
|
|
|
|
|
|
|
|
|
|
return `${summary}\n\n${stacktraceSection}`;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|