Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 455e6f1

Browse filesBrowse files
committed
util: throw toJSON errors when formatting %j
Previously all errors resulting from JSON.stringify were treated as a proof for circularity of the object structure. That is not the case if the `toJSON` method of the object throws an error. Explicitly check for the exact error message when determining the object structure's cyclicity. PR-URL: #11708 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 98e54b0 commit 455e6f1
Copy full SHA for 455e6f1

File tree

Expand file treeCollapse file tree

2 files changed

+16
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+16
-2
lines changed
Open diff view settings
Collapse file

‎lib/util.js‎

Copy file name to clipboardExpand all lines: lib/util.js
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ const inspectDefaultOptions = Object.seal({
3838
breakLength: 60
3939
});
4040

41+
const CIRCULAR_ERROR_MESSAGE = 'Converting circular structure to JSON';
42+
4143
var Debug;
4244

4345
function tryStringify(arg) {
4446
try {
4547
return JSON.stringify(arg);
46-
} catch (_) {
47-
return '[Circular]';
48+
} catch (err) {
49+
if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE)
50+
return '[Circular]';
51+
throw err;
4852
}
4953
}
5054

Collapse file

‎test/parallel/test-util-format.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-format.js
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ assert.strictEqual(util.format('o: %j, a: %j'), 'o: %j, a: %j');
8686
assert.strictEqual(util.format('%j', o), '[Circular]');
8787
}
8888

89+
{
90+
const o = {
91+
toJSON() {
92+
throw new Error('Not a circular object but still not serializable');
93+
}
94+
};
95+
assert.throws(() => util.format('%j', o),
96+
/^Error: Not a circular object but still not serializable$/);
97+
}
98+
8999
// Errors
90100
const err = new Error('foo');
91101
assert.strictEqual(util.format(err), err.stack);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.