Created
May 28, 2017 13:22
-
-
Save max-lt/121d6da979d709c8d54deb1457b52fb9 to your computer and use it in GitHub Desktop.
Error factory in ES6
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
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