Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
addressing review comments
  • Loading branch information
abelginrayen
abelginrayen committed Nov 10, 2018
commit d18e25ecb8bb141bc21020c518c59bbb86867550
30 changes: 8 additions & 22 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,9 @@ function formatRaw(ctx, value, recurseTimes) {
}
base = dateToISOString(value);
} else if (isError(value)) {
const prefix = getPrefix(constructor, '', 'Error');
// Make error with message first say the error.
base = formatError(value, getPrefix(constructor, '', 'Error'));
base = formatError(value, prefix);
// Wrap the error in brackets in case it has no stack trace.
const stackStart = base.indexOf('\n at');
if (stackStart === -1) {
Expand All @@ -652,8 +653,7 @@ function formatRaw(ctx, value, recurseTimes) {
// The message and the stack have to be indented as well!
if (ctx.indentationLvl !== 0) {
const indentation = ' '.repeat(ctx.indentationLvl);
base = formatError(value, getPrefix(constructor, '', 'Error'))
.replace(/\n/g, `\n${indentation}`);
base = formatError(value, prefix).replace(/\n/g, `\n${indentation}`);
}
if (keys.length === 0)
return base;
Expand Down Expand Up @@ -863,27 +863,13 @@ function formatPrimitive(fn, value, ctx) {
}

function formatError(value, constructorName) {

if (value.stack) {
let stack = value.stack;
if (!stack.startsWith(constructorName) &&
!(stack.startsWith('file:') && !constructorName.startsWith('file'))) {
const stackStart = value.stack.indexOf('\n at');
const errorAndMessage = value.stack.slice(0, stackStart);
const messageStart = errorAndMessage.indexOf(':');
if (stack && stackStart !== -1) {
stack = stack.slice(stackStart, stack.length);
if (messageStart !== -1) {
const message = errorAndMessage.slice(messageStart + 2,
stackStart.length);
// Reconstruct the stack
stack = constructorName.trim() + ': ' + message.trim() + stack;
} else {
stack = constructorName.trim() + stack;
}
}

return stack;
const parent = Object.getPrototypeOf(value);
if (!stack.startsWith(constructorName)) {
stack = stack.replace(parent, constructorName.trim());
} else if (parent === null) {
stack = stack.replace('Error', constructorName.trim());
}
return stack;
}
Expand Down