Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: add support for error subclassing and null prototype #23852

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
33 changes: 29 additions & 4 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ function formatRaw(ctx, value, recurseTimes) {
base = dateToISOString(value);
} else if (isError(value)) {
// Make error with message first say the error.
base = formatError(value);
base = formatError(value, getPrefix(constructor, '', 'Error'));
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
// Wrap the error in brackets in case it has no stack trace.
const stackStart = base.indexOf('\n at');
if (stackStart === -1) {
Expand All @@ -652,7 +652,8 @@ 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).replace(/\n/g, `\n${indentation}`);
base = formatError(value, getPrefix(constructor, '', 'Error'))
.replace(/\n/g, `\n${indentation}`);
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
}
if (keys.length === 0)
return base;
Expand Down Expand Up @@ -861,8 +862,32 @@ function formatPrimitive(fn, value, ctx) {
return fn(value.toString(), 'symbol');
}

function formatError(value) {
return value.stack || errorToString(value);
function formatError(value, constructorName) {

BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
if (value.stack) {
let stack = value.stack;
if (!stack.startsWith(constructorName) &&
!(stack.startsWith('file:') && !constructorName.startsWith('file'))) {
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
const stackStart = value.stack.indexOf('\n at');
const errorAndMessage = value.stack.slice(0, stackStart);
const messageStart = errorAndMessage.indexOf(':');
if (stack && stackStart !== -1) {
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
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;
}
return stack;
}
return errorToString(value);
}

function formatNamespaceObject(ctx, value, recurseTimes, keys) {
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1772,3 +1772,37 @@ assert.strictEqual(
});
assert.strictEqual(util.inspect(obj), '[Set: null prototype] { 1, 2 }');
}

// error subclassing and null prototype checks
{
const error = new Error('My Message');
assert.ok(util.inspect(error).includes('Error: My Message'));

Object.setPrototypeOf(error, null);
assert.ok(util.inspect(error)
.includes('[Error: null prototype]: My Message'));
}

{
class MyCustomError extends Error {}
const customErr = new MyCustomError('custom message');
assert.ok(util.inspect(customErr)
.includes('MyCustomError: custom message'));

Object.setPrototypeOf(customErr, null);
assert.ok(util.inspect(customErr)
.includes('[Error: null prototype]: custom message'));
}

// Check custom errors with name `File`
// works
{
class FileCustomError extends Error {}
const customErr = new FileCustomError();
assert.ok(util.inspect(customErr)
.includes('FileCustomError'));

Object.setPrototypeOf(customErr, null);
assert.ok(util.inspect(customErr)
.includes('[Error: null prototype]'));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is a duplicate of the test above.