Last active
May 24, 2016 11:53
-
-
Save molily/ed1420978a18108ff5c39da200bfe38c to your computer and use it in GitHub Desktop.
Custom error class that inherits from Error
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
// 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