Skip to content

Instantly share code, notes, and snippets.

@mrister
Created August 5, 2016 11:10
Show Gist options
  • Save mrister/b04e91a643001894b87e20b7fe179534 to your computer and use it in GitHub Desktop.
Save mrister/b04e91a643001894b87e20b7fe179534 to your computer and use it in GitHub Desktop.
A simple custom Node.js ES6 Error
/**
* A custom MyError class
* @class
*/
class MyError extends Error {
/**
* Constructs the MyError class
* @param {String} message an error message
* @constructor
*/
constructor(message) {
super(message);
// properly capture stack trace in Node.js
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
}
}
// test it
throw new MyError('test');
//MyError: test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment