Skip to content

Commit 4eb194a

Browse files
kunalspathakjasnell
authored andcommitted
lib: Use regex to compare error message
To make node engine agnostic, use better comparison method for error message. Lazily populate the `circular reference` error message thrown by `JSON.stringify()` which can be used to compare the error message thrown. PR-URL: #11854 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent 86d74a2 commit 4eb194a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib/util.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,21 @@ const inspectDefaultOptions = Object.seal({
3838
breakLength: 60
3939
});
4040

41-
const CIRCULAR_ERROR_MESSAGE = 'Converting circular structure to JSON';
42-
41+
var CIRCULAR_ERROR_MESSAGE;
4342
var Debug;
4443

4544
function tryStringify(arg) {
4645
try {
4746
return JSON.stringify(arg);
4847
} catch (err) {
48+
// Populate the circular error message lazily
49+
if (!CIRCULAR_ERROR_MESSAGE) {
50+
try {
51+
const a = {}; a.a = a; JSON.stringify(a);
52+
} catch (err) {
53+
CIRCULAR_ERROR_MESSAGE = err.message;
54+
}
55+
}
4956
if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE)
5057
return '[Circular]';
5158
throw err;

0 commit comments

Comments
 (0)