Skip to content

Commit

Permalink
fixup! util: always visualize cause property in errors during inspection
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Bridgewater <[email protected]>
  • Loading branch information
BridgeAR committed Nov 29, 2021
1 parent a795424 commit 75ff820
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 20 deletions.
38 changes: 21 additions & 17 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1274,27 +1274,31 @@ function formatError(err, constructor, tag, ctx, keys) {
const stackStart = stack.indexOf('\n at', pos);
if (stackStart === -1) {
stack = `[${stack}]`;
} else if (ctx.colors) {
// Highlight userland code and node modules.
} else {
let newStack = stack.slice(0, stackStart);
const lines = getStackFrames(ctx, err, stack.slice(stackStart + 1));
for (const line of lines) {
const core = line.match(coreModuleRegExp);
if (core !== null && NativeModule.exists(core[1])) {
newStack += `\n${ctx.stylize(line, 'undefined')}`;
} else {
// This adds underscores to all node_modules to quickly identify them.
let nodeModule;
newStack += '\n';
let pos = 0;
while (nodeModule = nodeModulesRegExp.exec(line)) {
// '/node_modules/'.length === 14
newStack += line.slice(pos, nodeModule.index + 14);
newStack += ctx.stylize(nodeModule[1], 'module');
pos = nodeModule.index + nodeModule[0].length;
if (ctx.colors) {
// Highlight userland code and node modules.
for (const line of lines) {
const core = line.match(coreModuleRegExp);
if (core !== null && NativeModule.exists(core[1])) {
newStack += `\n${ctx.stylize(line, 'undefined')}`;
} else {
// This adds underscores to all node_modules to quickly identify them.
let nodeModule;
newStack += '\n';
let pos = 0;
while (nodeModule = nodeModulesRegExp.exec(line)) {
// '/node_modules/'.length === 14
newStack += line.slice(pos, nodeModule.index + 14);
newStack += ctx.stylize(nodeModule[1], 'module');
pos = nodeModule.index + nodeModule[0].length;
}
newStack += pos === 0 ? line : line.slice(pos);
}
newStack += pos === 0 ? line : line.slice(pos);
}
} else {
newStack += `\n${lines.join('\n')}`;
}
stack = newStack;
}
Expand Down
16 changes: 13 additions & 3 deletions test/message/util-inspect-error-cause.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require('../common');

const { inspect } = require('util');

class FoobarError extends Error {
status = 'Feeling good';
}
Expand All @@ -15,7 +17,15 @@ process.nextTick(() => {
const error = new RangeError('New Stack Frames', { cause: cause2 });
const error2 = new RangeError('New Stack Frames', { cause: cause3 });

console.log(error);
console.log(cause3);
console.log(error2);
inspect.defaultOptions.colors = true;

console.log(inspect(error));
console.log(inspect(cause3));
console.log(inspect(error2));

inspect.defaultOptions.colors = false;

console.log(inspect(error));
console.log(inspect(cause3));
console.log(inspect(error2));
});
68 changes: 68 additions & 0 deletions test/message/util-inspect-error-cause.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,71 @@
RangeError: New Stack Frames
at *
*[90m at *[39m {
[cause]: FoobarError: Individual message
at *
*[90m at *[39m
*[90m ... 4 lines matching cause stack trace ...*[39m
*[90m at *[39m {
status: *[32m'Feeling good'*[39m,
extraProperties: *[32m'Yes!'*[39m,
[cause]: TypeError: Inner error
at *
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
}
}
Error: Stack causes
at *
*[90m at *[39m
*[90m ... 4 lines matching cause stack trace ...*[39m
*[90m at *[39m {
[cause]: FoobarError: Individual message
at *
*[90m at *[39m
*[90m ... 4 lines matching cause stack trace ...*[39m
*[90m at *[39m {
status: *[32m'Feeling good'*[39m,
extraProperties: *[32m'Yes!'*[39m,
[cause]: TypeError: Inner error
at *
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
}
}
RangeError: New Stack Frames
at *
*[90m at *[39m {
[cause]: Error: Stack causes
at *
*[90m at *[39m
*[90m ... 4 lines matching cause stack trace ...*[39m
*[90m at *[39m {
[cause]: FoobarError: Individual message
at *
*[90m at *[39m
*[90m ... 4 lines matching cause stack trace ...*[39m
*[90m at *[39m {
status: *[32m'Feeling good'*[39m,
extraProperties: *[32m'Yes!'*[39m,
[cause]: TypeError: Inner error
at *
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
}
}
}
RangeError: New Stack Frames
at *
at * {
Expand Down

0 comments on commit 75ff820

Please sign in to comment.