Skip to content

Instantly share code, notes, and snippets.

@molily
Last active May 24, 2016 11:53
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 molily/ed1420978a18108ff5c39da200bfe38c to your computer and use it in GitHub Desktop.
Save molily/ed1420978a18108ff5c39da200bfe38c to your computer and use it in GitHub Desktop.
Custom error class that inherits from Error
// Manual inheritance without super call. We can’t use `class` declaration
// since the Babel compilation output will call Error.call(this, arguments),
// which always returns a new Error instance instead of the instance.
// See http://www.ecma-international.org/ecma-262/6.0/#sec-error-constructor
// Apart from this change, we’re using Babel’s approach of inheritance:
// https://github.com/babel/babel/blob/v6.9.0/packages/babel-helpers/src/helpers.js#L210-L225
/* eslint-disable func-style */
function CustomError(message) {
// NO super call here
this.message = message;
}
/* eslint-enable func-style */
CustomError.prototype = Object.create(Error.prototype, {
constructor: {
value: CustomError,
enumerable: false,
writable: true,
configurable: true
},
name: {
value: 'CustomError',
enumerable: true,
writable: false,
configurable: false
}
});
// Inherit static methods
if (Object.setPrototypeOf) {
Object.setPrototypeOf(CustomError, Error);
} else {
/* eslint-disable no-proto */
CustomError.__proto__ = Error;
/* eslint-enable no-proto */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment