Skip to content

Commit 71a0d7d

Browse files
committed
fixup! util: use more defensive code when inspecting error objects
1 parent adc4003 commit 71a0d7d

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

lib/internal/util/inspect.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,41 +1914,46 @@ function formatError(err, constructor, tag, ctx, keys) {
19141914
let message, name, stack;
19151915
try {
19161916
stack = getStackString(ctx, err);
1917-
if (!ctx.showHidden && keys.length !== 0) {
1918-
const index = ArrayPrototypeIndexOf(keys, 'stack');
1919-
// Only hide the property if it's a string and if it's part of the original stack
1920-
if (index !== -1) {
1921-
ArrayPrototypeSplice(keys, index, 1);
1922-
}
1923-
}
19241917
} catch {
19251918
return ObjectPrototypeToString(err);
19261919
}
1920+
1921+
let messageIsGetterThatThrows = false;
19271922
try {
19281923
message = err.message;
1929-
if (!ctx.showHidden && keys.length !== 0) {
1924+
} catch (err2) {
1925+
messageIsGetterThatThrows = true;
1926+
}
1927+
let nameIsGetterThatThrows = false;
1928+
try {
1929+
name = err.name;
1930+
} catch {
1931+
nameIsGetterThatThrows = true;
1932+
}
1933+
1934+
if (!ctx.showHidden && keys.length !== 0) {
1935+
const index = ArrayPrototypeIndexOf(keys, 'stack');
1936+
if (index !== -1) {
1937+
ArrayPrototypeSplice(keys, index, 1);
1938+
}
1939+
1940+
if (!messageIsGetterThatThrows) {
19301941
const index = ArrayPrototypeIndexOf(keys, 'message');
19311942
// Only hide the property if it's a string and if it's part of the original stack
19321943
if (index !== -1 && (typeof message !== 'string' || StringPrototypeIncludes(stack, message))) {
19331944
ArrayPrototypeSplice(keys, index, 1);
19341945
}
19351946
}
1936-
} catch {
1937-
// If message is a getter that throws, we ignore the error.
1938-
}
1939-
try {
1940-
name = err.name;
1941-
if (!ctx.showHidden && keys.length !== 0) {
1947+
1948+
if (!nameIsGetterThatThrows) {
19421949
const index = ArrayPrototypeIndexOf(keys, 'name');
19431950
// Only hide the property if it's a string and if it's part of the original stack
19441951
if (index !== -1 && (typeof name !== 'string' || StringPrototypeIncludes(stack, name))) {
19451952
ArrayPrototypeSplice(keys, index, 1);
19461953
}
19471954
}
1948-
name ??= 'Error';
1949-
} catch {
1950-
name = 'Error';
19511955
}
1956+
name ??= 'Error';
19521957

19531958
if ('cause' in err &&
19541959
(keys.length === 0 || !ArrayPrototypeIncludes(keys, 'cause'))) {

0 commit comments

Comments
 (0)