Created
September 8, 2015 10:09
-
-
Save dented/88588dabba1c70f35dc6 to your computer and use it in GitHub Desktop.
Custom Error Constructor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Error.custromConstructor = (function() { | |
function define(obj, prop, value) { | |
Object.defineProperty(obj, prop, { | |
value: value, | |
configurable: true, | |
enumerable: false, | |
writable: true | |
}); | |
} | |
return function(name, init, proto) { | |
var CustomError; | |
proto = proto || {}; | |
function build(message) { | |
var self = this instanceof CustomError | |
? this | |
: Object.create(CustomError.prototype); | |
Error.apply(self, arguments); | |
Error.captureStackTrace(self, CustomError); | |
if (message != undefined) { | |
define(self, 'message', String(message)); | |
} | |
define(self, 'arguments', undefined); | |
define(self, 'type', undefined); | |
if (typeof init == 'function') { | |
init.apply(self, arguments); | |
} | |
return self; | |
} | |
eval('CustomError = function ' + name + '() {' + | |
'return build.apply(this, arguments); }'); | |
CustomError.prototype = Object.create(Error.prototype); | |
define(CustomError.prototype, 'constructor', CustomError); | |
for (var key in proto) { | |
define(CustomError.prototype, key, proto[key]); | |
} | |
Object.defineProperty(CustomError.prototype, 'name', { value: name }); | |
return CustomError; | |
} | |
})(); | |
/** | |
* name The name of the constructor name | |
* init User-defined initialization function | |
* proto It's enumerable members will be added to | |
* prototype of created constructor | |
**/ | |
Error.custromConstructor = function(name, init, proto) | |
var NotImplementedError = Error.custromConstructor('NotImplementedError'); | |
throw new NotImplementedError(); | |
var err = new NotImplementedError(); | |
var err = NotImplementedError('Not yet...'); | |
// Different Version | |
function InvalidArgumentException(message) { | |
this.message = message; | |
// Use V8's native method if available, otherwise fallback | |
if ("captureStackTrace" in Error) | |
Error.captureStackTrace(this, InvalidArgumentException); | |
else | |
this.stack = (new Error()).stack; | |
} | |
InvalidArgumentException.prototype = Object.create(Error.prototype); | |
InvalidArgumentException.prototype.name = "InvalidArgumentException"; | |
InvalidArgumentException.prototype.constructor = InvalidArgumentException; | |
throw new InvalidArgumentException(); | |
var err = new InvalidArgumentException("Not yet..."); | |
err instanceof InvalidArgumentException // -> true | |
err instanceof Error // -> true | |
InvalidArgumentException.prototype.isPrototypeOf(err) // -> true | |
Error.prototype.isPrototypeOf(err) // -> true | |
err.constructor.name // -> InvalidArgumentException | |
err.name // -> InvalidArgumentException | |
err.message // -> Not yet... | |
err.toString() // -> InvalidArgumentException: Not yet... | |
err.stack // -> works fine! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment