Created
February 9, 2015 23:19
-
-
Save ckknight/89eacc015abe052ef3d5 to your computer and use it in GitHub Desktop.
CustomError
This file contains 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
// replace `CustomError` with the name of your error | |
// replace `ParentError` with the superclass, even if it is `Error` or another native error. | |
// other arguments besides `message` can be added, but at least `message` should exist. | |
var CustomError = (function (ParentError) { | |
function CustomError(message) { | |
var self = this instanceof CustomError ? this : Object.create(CustomError.prototype); | |
var err = ParentError.call(self, message); | |
self.message = message; | |
if (typeof Error.captureStackTrace === 'function') { | |
Error.captureStackTrace(self, CustomError); | |
} else if ('stack' in err) { | |
self.stack = err.stack; | |
} | |
return self; | |
} | |
CustomError.prototype = Object.create(ParentError.prototype); | |
CustomError.prototype.constructor = CustomError; | |
CustomError.prototype.name = 'CustomError'; | |
return CustomError; | |
}(ParentError)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment