From 562be6d8446b19b3e5e4831da43497364e19f235 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Tue, 14 Mar 2017 17:48:28 -0700 Subject: [PATCH 1/3] lib: Use regex to compare error message To make node engine agnostic, use better comparison method for error message. --- lib/util.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index 31761f0dd35545..09128e3109260c 100644 --- a/lib/util.js +++ b/lib/util.js @@ -38,7 +38,7 @@ const inspectDefaultOptions = Object.seal({ breakLength: 60 }); -const CIRCULAR_ERROR_MESSAGE = 'Converting circular structure to JSON'; +const CIRCULAR_ERROR_MESSAGE = /Circular/i; var Debug; @@ -46,7 +46,7 @@ function tryStringify(arg) { try { return JSON.stringify(arg); } catch (err) { - if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE) + if (err.name === 'TypeError' && err.message.match(CIRCULAR_ERROR_MESSAGE)) return '[Circular]'; throw err; } From 83109a9b7b262b3372da975afc84ef4934d48d1d Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Fri, 17 Mar 2017 16:09:15 -0700 Subject: [PATCH 2/3] lib: Addressed review comments Lazily populate the `circular reference` error message thrown by `JSON.stringify()` which can be used to compare the error message thrown. --- lib/util.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index 09128e3109260c..69c29598f29b90 100644 --- a/lib/util.js +++ b/lib/util.js @@ -38,14 +38,21 @@ const inspectDefaultOptions = Object.seal({ breakLength: 60 }); -const CIRCULAR_ERROR_MESSAGE = /Circular/i; - +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.match(CIRCULAR_ERROR_MESSAGE)) return '[Circular]'; throw err; From 4d1d9016922f3d41fa265c99b06e9a49e4d30b3e Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Fri, 17 Mar 2017 16:30:08 -0700 Subject: [PATCH 3/3] lib: Addressed review comments --- lib/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index 69c29598f29b90..1de8f216c83218 100644 --- a/lib/util.js +++ b/lib/util.js @@ -53,7 +53,7 @@ function tryStringify(arg) { CIRCULAR_ERROR_MESSAGE = err.message; } } - if (err.name === 'TypeError' && err.message.match(CIRCULAR_ERROR_MESSAGE)) + if (err.name === 'TypeError' && err.message === CIRCULAR_ERROR_MESSAGE) return '[Circular]'; throw err; }