From 143eae2b89a669923ec01c9f27d147f8122acfdd Mon Sep 17 00:00:00 2001 From: Dan Brakeley Date: Tue, 19 Aug 2014 12:01:57 -0400 Subject: [PATCH] catch exceptions that occur in JSON.stringify() when printing test failures --- lib/assert.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 55ef74493..51b1b294b 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -86,10 +86,30 @@ assert.AssertionError.prototype.toString = function() { if (this.message) { return [this.name+":", this.message].join(' '); } else { + var expectedString; + try { + expectedString = (typeof this.expected !== 'undefined' + ? JSON.stringify(this.expected) + : 'undefined'); + } + catch (e) { + return this.name + ": JSON.stringify(expected) threw exception: \"" + e + "\""; + } + + var actualString; + try { + actualString = (typeof this.actual !== 'undefined' + ? JSON.stringify(this.actual) + : 'undefined'); + } + catch (e) { + return this.name + ": JSON.stringify(actual) threw exception: \"" + e + "\""; + } + return [ this.name+":" - , typeof this.expected !== 'undefined' ? JSON.stringify(this.expected) : 'undefined' + , expectedString , this.operator - , typeof this.actual !== 'undefined' ? JSON.stringify(this.actual) : 'undefined' + , actualString ].join(" "); } };