Last active
August 29, 2015 13:57
-
-
Save wtfil/9619652 to your computer and use it in GitHub Desktop.
Right way to extend Error
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
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