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