Skip to content

Commit 80e8f30

Browse files
committed
fixup! util: rely in inspect to describe error
1 parent 9b05e4e commit 80e8f30

File tree

2 files changed

+24
-46
lines changed

2 files changed

+24
-46
lines changed

lib/internal/util/inspect.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ const {
156156
} = require('internal/util');
157157

158158
const {
159-
isErrorStackTraceLimitWritable,
160159
isStackOverflowError,
161160
} = require('internal/errors');
162161

@@ -2558,18 +2557,7 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc,
25582557
}
25592558
ctx.indentationLvl -= 2;
25602559
} catch (err) {
2561-
// Error message itself may be a getter that throws
2562-
let messageSuffix = '';
2563-
if (isErrorStackTraceLimitWritable()) {
2564-
const stackTraceLimit = Error.stackTraceLimit;
2565-
Error.stackTraceLimit = 0;
2566-
try {
2567-
messageSuffix = ` (${inspect(err)})`;
2568-
// eslint-disable-next-line no-empty
2569-
} catch { } finally { Error.stackTraceLimit = stackTraceLimit; }
2570-
}
2571-
2572-
const message = `<Inspection threw${messageSuffix}>`;
2560+
const message = `<Inspection threw (${inspect(err)})>`;
25732561
str = `${s(`[${label}:`, sp)} ${message}${s(']', sp)}`;
25742562
}
25752563
} else {

test/parallel/test-util-inspect.js

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
690690

691691
{
692692
const tmp = Error.stackTraceLimit;
693-
Error.stackTraceLimit = 0;
693+
Object.defineProperty(Error, 'stackTraceLimit', { value: 0, enumerable: false });
694694
const err = new Error('foo');
695695
const err2 = new Error('foo\nbar');
696696
assert.strictEqual(util.inspect(err, { compact: true }), '[Error: foo]');
@@ -2586,6 +2586,7 @@ assert.strictEqual(
25862586

25872587
Object.defineProperties(error, {
25882588
name: throwingGetter,
2589+
message: throwingGetter,
25892590
stack: throwingGetter,
25902591
cause: throwingGetter,
25912592
});
@@ -2600,6 +2601,7 @@ assert.strictEqual(
26002601
' foo: [Getter: <Inspection threw ([object Error] {\n' +
26012602
' stack: [Getter/Setter],\n' +
26022603
' name: [Getter],\n' +
2604+
' message: [Getter],\n' +
26032605
' cause: [Getter]\n' +
26042606
'})>]\n' +
26052607
'}',
@@ -2654,9 +2656,7 @@ assert.strictEqual(
26542656
inspect({
26552657
get foo() { throw Error; }
26562658
}, { getters: true }),
2657-
'{\n' +
2658-
' foo: [Getter: <Inspection threw ([Function: Error] { stackTraceLimit: 0 })>]\n' +
2659-
'}'
2659+
'{ foo: [Getter: <Inspection threw ([Function: Error])>] }'
26602660
);
26612661
}
26622662

@@ -3341,36 +3341,26 @@ assert.strictEqual(
33413341
'\x1B[2mdef: \x1B[33m5\x1B[39m\x1B[22m }'
33423342
);
33433343

3344-
assert.strictEqual(
3344+
assert.match(
33453345
inspect(Object.getPrototypeOf(bar), { showHidden: true, getters: true }),
3346-
'<ref *1> Foo [Map] {\n' +
3347-
' [constructor]: [class Bar extends Foo] {\n' +
3348-
' [length]: 0,\n' +
3349-
" [name]: 'Bar',\n" +
3350-
' [prototype]: [Circular *1],\n' +
3351-
' [Symbol(Symbol.species)]: [Getter: <Inspection threw ' +
3352-
// Heh? I don't know how to override the error stack to make it predictable
3353-
"(TypeError: Symbol.prototype.toString requires that 'this' be a Symbol\n" +
3354-
' at Bar.toString (<anonymous>)\n' +
3355-
' at formatPrimitive (node:internal/util/inspect:2246:13)\n' +
3356-
' at formatProperty (node:internal/util/inspect:2556:29)\n' +
3357-
' at addPrototypeProperties (node:internal/util/inspect:1010:21)\n' +
3358-
' at getConstructorName (node:internal/util/inspect:918:11)\n' +
3359-
' at formatRaw (node:internal/util/inspect:1194:23)\n' +
3360-
' at formatValue (node:internal/util/inspect:1184:10)\n' +
3361-
' at formatProperty (node:internal/util/inspect:2536:11)\n' +
3362-
' at formatRaw (node:internal/util/inspect:1429:9)\n' +
3363-
' at formatValue (node:internal/util/inspect:1184:10))>]\n' +
3364-
' },\n' +
3365-
" [xyz]: [Getter: 'YES!'],\n" +
3366-
' [Symbol(nodejs.util.inspect.custom)]: ' +
3367-
'[Function: [nodejs.util.inspect.custom]] {\n' +
3368-
' [length]: 0,\n' +
3369-
" [name]: '[nodejs.util.inspect.custom]'\n" +
3370-
' },\n' +
3371-
' [abc]: [Getter: true],\n' +
3372-
' [def]: [Getter/Setter: false]\n' +
3373-
' }'
3346+
new RegExp('^' + RegExp.escape(
3347+
'<ref *1> Foo [Map] {\n' +
3348+
' [constructor]: [class Bar extends Foo] {\n' +
3349+
' [length]: 0,\n' +
3350+
" [name]: 'Bar',\n" +
3351+
' [prototype]: [Circular *1],\n' +
3352+
' [Symbol(Symbol.species)]: [Getter: <Inspection threw ' +
3353+
"(TypeError: Symbol.prototype.toString requires that 'this' be a Symbol") + '.*' + RegExp.escape(')>]\n' +
3354+
' },\n' +
3355+
" [xyz]: [Getter: 'YES!'],\n" +
3356+
' [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]] {\n' +
3357+
' [length]: 0,\n' +
3358+
" [name]: '[nodejs.util.inspect.custom]'\n" +
3359+
' },\n' +
3360+
' [abc]: [Getter: true],\n' +
3361+
' [def]: [Getter/Setter: false]\n' +
3362+
' }'
3363+
) + '$', 's')
33743364
);
33753365

33763366
assert.strictEqual(

0 commit comments

Comments
 (0)