1
0
Fork 0

Altered warning display and stack trace

closure/master
Mike Gerwitz 2011-07-06 17:55:40 -04:00
parent 3820ee4b07
commit 1b1a4b60d5
2 changed files with 26 additions and 4 deletions

View File

@ -53,12 +53,24 @@ var Warning = exports.Warning = function( e )
throw TypeError( "Must provide exception to wrap" ); throw TypeError( "Must provide exception to wrap" );
} }
Error.prototype.constructor.call( this, e.message );
// copy over the message for convenience // copy over the message for convenience
this.message = e.message; this.message = e.message;
this.name = 'Warning';
this._error = e; 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 = Error();
Warning.prototype.constructor = Warning;
Warning.prototype.name = 'Warning';
/** /**

View File

@ -29,17 +29,27 @@ var common = require( './common' ),
/** /**
* Warning's prototype should be Error to ensure instanceof() checks work * Warning should be a subtype of Error
* properly
*/ */
( function testWarningIsAvailableAndHasErrorAsPrototype() ( function testWarningIsAvailableAndHasErrorPrototype()
{ {
assert.ok( ( Warning.prototype instanceof Error ), assert.ok( ( Warning( Error() ) instanceof Error ),
"Warning should be an instance of 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 * Just as with the other Error classes, as well as all ease.js classes, the
* 'new' keyword should be optional when instantiating the class * 'new' keyword should be optional when instantiating the class