Skip to content

Commit

Permalink
lib: Use regex to compare error message
Browse files Browse the repository at this point in the history
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 <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>
  • Loading branch information
kunalspathak authored and jasnell committed Mar 22, 2017
1 parent 86d74a2 commit 4eb194a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4eb194a

Please sign in to comment.