From 1b1a4b60d5516cdcde2fe741be152f134f8e6acc Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 6 Jul 2011 17:55:40 -0400 Subject: [PATCH] Altered warning display and stack trace --- lib/warn.js | 12 ++++++++++++ test/test-warn-exception.js | 18 ++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/warn.js b/lib/warn.js index af45948..7a72bc6 100644 --- a/lib/warn.js +++ b/lib/warn.js @@ -53,12 +53,24 @@ var Warning = exports.Warning = function( e ) throw TypeError( "Must provide exception to wrap" ); } + Error.prototype.constructor.call( this, e.message ); + // copy over the message for convenience this.message = e.message; + this.name = 'Warning'; this._error = e; + + this.stack = this.stack && + this.stack.replace( /^.*?\n+/, + this.name + ': ' + this.message + "\n" + ); }; +// ensures the closest compatibility...just be careful not to modify Warning's +// prototype Warning.prototype = Error(); +Warning.prototype.constructor = Warning; +Warning.prototype.name = 'Warning'; /** diff --git a/test/test-warn-exception.js b/test/test-warn-exception.js index 72cac1a..c0fa30a 100644 --- a/test/test-warn-exception.js +++ b/test/test-warn-exception.js @@ -29,17 +29,27 @@ var common = require( './common' ), /** - * Warning's prototype should be Error to ensure instanceof() checks work - * properly + * Warning should be a subtype of Error */ -( function testWarningIsAvailableAndHasErrorAsPrototype() +( function testWarningIsAvailableAndHasErrorPrototype() { - assert.ok( ( Warning.prototype instanceof Error ), + assert.ok( ( Warning( Error() ) instanceof Error ), "Warning should be an instance of Error" ); } )(); +/** + * Make clear that we're working with a warning + */ +( function testWarningShouldAlterErrorName() +{ + assert.equal( Warning( Error() ).name, 'Warning', + "Warning name should be properly set" + ); +} )(); + + /** * Just as with the other Error classes, as well as all ease.js classes, the * 'new' keyword should be optional when instantiating the class