Skip to content

Instantly share code, notes, and snippets.

@wtfil
Last active August 29, 2015 13:57
Show Gist options
  • Save wtfil/9619652 to your computer and use it in GitHub Desktop.
Save wtfil/9619652 to your computer and use it in GitHub Desktop.
Right way to extend Error
var CustomError = function(message, anotherField) {
this.message = message;
this.anotherField = anotherField;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, CustomError);
} else {
var e = new Error;
this.fileName = e.fileName;
this.lineNumber = e.lineNumber;
this.stack = e.stack.split('\n').slice(1).join('\n')
}
};
CustomError.prototype = Object.create(Error.prototype);
CustomError.prototype.name = 'CustomError';
// var e = new CustomError('olol');
// throw e;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment