Skip to content
Permalink
Browse files

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 <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 15, 2017
1 parent 86d74a2 commit 4eb194a2b10194f3ac1a4c8914d725aa90da265e
Showing with 9 additions and 2 deletions.
  1. +9 −2 lib/util.js
@@ -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;

0 comments on commit 4eb194a

Please sign in to comment.
You can’t perform that action at this time.