0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00

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: https://github.com/nodejs/node/pull/11854
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
This commit is contained in:
Kunal Pathak 2017-03-14 17:48:28 -07:00 committed by James M Snell
parent 86d74a248b
commit 4eb194a2b1

View File

@ -38,14 +38,21 @@ const inspectDefaultOptions = Object.seal({
breakLength: 60
});
const CIRCULAR_ERROR_MESSAGE = 'Converting circular structure to JSON';
var CIRCULAR_ERROR_MESSAGE;
var Debug;
function tryStringify(arg) {
try {
return JSON.stringify(arg);
} catch (err) {
// Populate the circular error message lazily
if (!CIRCULAR_ERROR_MESSAGE) {
try {
const a = {}; a.a = a; JSON.stringify(a);
} catch (err) {
CIRCULAR_ERROR_MESSAGE = err.message;
}
}
if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE)
return '[Circular]';
throw err;