Last active
November 5, 2018 19:08
-
-
Save mkropat/b4cd668f7960d76ee6dcf423cc8f1f57 to your computer and use it in GitHub Desktop.
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
| // For a runnable version, see: https://jsbin.com/caboqij/edit?js,console | |
| function MyError() { | |
| this._error = Error.apply(this, arguments); | |
| this.message = this._error.message; | |
| if (Error.captureStackTrace) { | |
| Error.captureStackTrace(this, MyError); | |
| } | |
| } | |
| MyError.prototype = Object.create(Error.prototype, { | |
| name: { | |
| value: 'MyError' | |
| }, | |
| stack: { | |
| get: function() { return this._error.stack; } | |
| } | |
| }); | |
| let err = new MyError('kaboom'); | |
| console.log(err.message); // kaboom | |
| console.log(err instanceof Error); // true | |
| console.log(err instanceof MyError); // true | |
| console.log(err.toString()); // MyError: kaboom | |
| console.log(err.stack); | |
| // MyError: kaboom | |
| // at <anonymous>:18:7 | |
| function MyInheritedError() { | |
| MyError.apply(this, arguments); | |
| if (Error.captureStackTrace) { | |
| Error.captureStackTrace(this, MyInheritedError); | |
| } | |
| } | |
| MyInheritedError.prototype = Object.create(MyError.prototype, { | |
| name: { | |
| value: 'MyInheritedError', | |
| } | |
| }); | |
| let err2 = new MyInheritedError('kaboom'); | |
| console.log(err2.message); // kaboom | |
| console.log(err2 instanceof Error); // true | |
| console.log(err2 instanceof MyError); // true | |
| console.log(err2 instanceof MyInheritedError); // true | |
| console.log(err2.toString()); // MyInheritedError: kaboom | |
| console.log(err2.stack); | |
| // MyInheritedError: kaboom | |
| // at <anonymous>:14:8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment