diff --git a/lib/util.js b/lib/util.js index b243c81dd50..80195c26722 100644 --- a/lib/util.js +++ b/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;