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 all commits
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
19 changes: 15 additions & 4 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);
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,7 +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).replace(/\n/g, `\n${indentation}`);
base = formatError(value, prefix).replace(/\n/g, `\n${indentation}`);
}
if (keys.length === 0)
return base;
Expand Down Expand Up @@ -861,8 +862,18 @@ function formatPrimitive(fn, value, ctx) {
return fn(value.toString(), 'symbol');
}

function formatError(value) {
return value.stack || errorToString(value);
function formatError(value, constructorName) {
if (value.stack) {
let stack = value.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;
}
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.