Last active
December 15, 2015 19:16
-
-
Save dlebech/6050bb04103f9ae627a7 to your computer and use it in GitHub Desktop.
Just testing out Error subclassing
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
'use strict'; | |
class MyError extends Error {} | |
class MyVerboseError extends Error { | |
constructor(message) { | |
super(message); | |
this.name = this.constructor.name; // ! important | |
this.message = message; | |
Error.captureStackTrace(this, this.name); | |
} | |
} | |
try { | |
throw new MyError('Simple error'); | |
} catch (e) { | |
console.log(e instanceof MyError); // true | |
console.log(e instanceof Error); // true | |
console.log(e.message); // "Simple error" | |
console.log(e.stack); | |
} | |
try { | |
throw new MyVerboseError('Verbose error'); | |
} catch (e) { | |
console.log(e instanceof MyVerboseError); // true | |
console.log(e instanceof Error); // true | |
console.log(e.message); // "Verbose error" | |
console.log(e.stack); | |
} | |
try { | |
throw new Error('Standard error'); | |
} catch (e) { | |
console.log(e instanceof MyError); // false | |
console.log(e instanceof MyVerboseError); // false | |
console.log(e instanceof Error); // true | |
console.log(e.message); // "Standard error" | |
console.log(e.stack); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment