Skip to content

Instantly share code, notes, and snippets.

@ckknight
Created February 9, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckknight/89eacc015abe052ef3d5 to your computer and use it in GitHub Desktop.
Save ckknight/89eacc015abe052ef3d5 to your computer and use it in GitHub Desktop.
CustomError
// 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