Skip to content

Instantly share code, notes, and snippets.

@max-lt
Created May 28, 2017 13:22
Show Gist options
  • Save max-lt/121d6da979d709c8d54deb1457b52fb9 to your computer and use it in GitHub Desktop.
Save max-lt/121d6da979d709c8d54deb1457b52fb9 to your computer and use it in GitHub Desktop.
Error factory in ES6
const assert = require('assert');
function makeTypedError(args) {
assert(args, 'TypedError: must specify options');
assert(args.type, 'TypedError: must specify options.type');
assert(args.message, 'TypedError: must specify options.message');
assert(args.statusCode, 'TypedError: must specify options.statusCode');
return (class extends Error {
constructor(message) {
super(message || args.message);
Object.defineProperty(this, 'name', {enumerable: false, value: args.type});
Object.defineProperty(this, 'type', {enumerable: false, value: args.type});
Object.defineProperty(this, 'statusCode', {enumerable: false, value: args.statusCode});
}
})
}
module.exports = makeTypedError;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment