Skip to content

Instantly share code, notes, and snippets.

@dlebech
Last active December 15, 2015 19:16
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 dlebech/6050bb04103f9ae627a7 to your computer and use it in GitHub Desktop.
Save dlebech/6050bb04103f9ae627a7 to your computer and use it in GitHub Desktop.
Just testing out Error subclassing
'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