Compare commits

..

No commits in common. '93be20f9c12db9bdc24a42e438f245ad382ea1be' and '2366b04dc7873105c51fadcd4de4084d9e14df1f' have entirely different histories.

@ -1,6 +1,6 @@
{ {
"name": "error-chain", "name": "error-chain",
"version": "0.1.2", "version": "0.1.1",
"main": "index.js", "main": "index.js",
"repository": "git@git.cryto.net:joepie91/node-error-chain.git", "repository": "git@git.cryto.net:joepie91/node-error-chain.git",
"author": "Sven Slootweg <admin@cryto.net>", "author": "Sven Slootweg <admin@cryto.net>",

@ -1,7 +1,6 @@
"use strict"; "use strict";
const chalk = require("chalk"); const chalk = require("chalk");
const syncpipe = require("syncpipe");
const { validateArguments } = require("@validatem/core"); const { validateArguments } = require("@validatem/core");
const required = require("@validatem/required"); const required = require("@validatem/required");
@ -13,50 +12,14 @@ const stripErrorFromStack = require("./strip-error-from-stack");
const isInstanceOf = require("./is-instance-of"); const isInstanceOf = require("./is-instance-of");
const getChain = require("../get-chain"); const getChain = require("../get-chain");
function prefixExtraLines(string, prefix) { function formattedErrorHeading(error, colorAll = false) {
return string let formattedMessage = error.message.trim().replace(/\n/g, " \\n ");
.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 formattedName = chalk.red(`${chalk.bold(error.name)}:`);
let coloredMessage = (colorAll === true) ? chalk.red(formattedMessage) : formattedMessage; let coloredMessage = (colorAll === true) ? chalk.red(formattedMessage) : formattedMessage;
return `${formattedName} ${coloredMessage}`; 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) { module.exports = function renderError(_error, _options) {
@ -71,41 +34,49 @@ module.exports = function renderError(_error, _options) {
let {allStacktraces} = options; let {allStacktraces} = options;
let errors = getChain(error); let errors = getChain(error);
let detailedErrorsToDisplay = (allStacktraces === true) ? errors : errors.slice(-1);
let summary = errors.map((error, i) => {
let prefix = (i > 0) ? "⤷ " : "";
/* IDEA: After every summarized error, add a summarized stacktrace; that is, a stacktrace that only contains the 'user code' entries that might point the developer at the source of the problem. To do that, we should filter out all node_modules and error-chain stuff, as well as internals like timers.js. Then, we should deduplicate lines across stacktraces, and hide the function name if it's boilerplate (eg. Promise.try.then stuff). Try this out with the 'rpm' regex in the CVM smartctl wrapper, as that covers all bases; duplication, stacktraces without user code, etc. */
// let cleanStack = error.stack.split("\n").filter((line) => {
// return (!line.includes("node_modules")
// && !line.includes("node-error-chain")
// && !line.includes("(timers.js")
// && !line.includes("<anonymous>")
// );
// }).join("\n");
// console.log(cleanStack);
return prefix + formattedErrorHeading(error);
}).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}`;
}).join("\n\n");
let stacktraceSection = (allStacktraces === true)
? `${chalk.cyan("All stacktraces:")}\n\n${stacktraces}`
: `${chalk.cyan("Stacktrace for original error:")}\n\n${stacktraces}`;
if (errors.length === 1) { return `${summary}\n\n${stacktraceSection}`;
// 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) => {
let prefix = (i > 0) ? "⤷ " : "";
/* IDEA: After every summarized error, add a summarized stacktrace; that is, a stacktrace that only contains the 'user code' entries that might point the developer at the source of the problem. To do that, we should filter out all node_modules and error-chain stuff, as well as internals like timers.js. Then, we should deduplicate lines across stacktraces, and hide the function name if it's boilerplate (eg. Promise.try.then stuff). Try this out with the 'rpm' regex in the CVM smartctl wrapper, as that covers all bases; duplication, stacktraces without user code, etc. */
// let cleanStack = error.stack.split("\n").filter((line) => {
// return (!line.includes("node_modules")
// && !line.includes("node-error-chain")
// && !line.includes("(timers.js")
// && !line.includes("<anonymous>")
// );
// }).join("\n");
// console.log(cleanStack);
return prefix + formattedErrorHeading(error, false, " ");
}).join("\n");
let stacktraces = detailedErrorsToDisplay.map((error, i) => {
let causedByPrefix = (i > 0 ? "Caused by: " : "");
let causedByPadding = (i > 0) ? " " : "";
return formattedError(error, causedByPadding, causedByPrefix);
}).join("\n\n");
let stacktraceSection = (allStacktraces === true)
? `${chalk.cyan("All stacktraces:")}\n\n${stacktraces}`
: `${chalk.cyan("Stacktrace for original error:")}\n\n${stacktraces}`;
return `${summary}\n\n${stacktraceSection}`;
}
}; };

Loading…
Cancel
Save