Skip to content

Instantly share code, notes, and snippets.

@CaptainLiao
Created February 8, 2018 04:04
Show Gist options
  • Save CaptainLiao/7d0ee3e0a165b83f60a806428df0818b to your computer and use it in GitHub Desktop.
Save CaptainLiao/7d0ee3e0a165b83f60a806428df0818b to your computer and use it in GitHub Desktop.
const UnimplementedError = createErrorType('Unimplemented');
const AuthError = createErrorType('AuthError');
const ResponseError = createErrorType('ResponseError', function(message, data) {
this.data = data;
});
module.exports = {
UnimplementedError,
AuthError,
ResponseError
};
function createErrorType(name, init) {
function CustomError(message) {
this.message = message;
init && init.apply(this, arguments);
}
CustomError.prototype = Object.create(Error.prototype, {
name: {
value: name
},
constructor: {
value: Error,
enumerable: false,
writable: true,
configurable: true
}
});
if (Object.setPrototypeOf) {
Object.setPrototypeOf(CustomError, Error);
} else {
CustomError.__proto__ = Error;
}
return CustomError;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment