diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 854b19918c16cd..dbcb8b1d5cf857 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -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; } diff --git a/test/message/util-inspect-error-cause.js b/test/message/util-inspect-error-cause.js index 109812fc3815ea..f6318357974cb6 100644 --- a/test/message/util-inspect-error-cause.js +++ b/test/message/util-inspect-error-cause.js @@ -2,6 +2,8 @@ require('../common'); +const { inspect } = require('util'); + class FoobarError extends Error { status = 'Feeling good'; } @@ -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)); }); diff --git a/test/message/util-inspect-error-cause.out b/test/message/util-inspect-error-cause.out index 1931ae822e60ec..bd8673ae25c031 100644 --- a/test/message/util-inspect-error-cause.out +++ b/test/message/util-inspect-error-cause.out @@ -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 * {