// Hello. There is now a module for this. // https://github.com/0x8890/error-class // $ npm install error-class const hasCaptureStackTrace = 'captureStackTrace' in Error // Internal function to set up an error. function setup (message) { const { constructor, constructor: { name } } = this if (hasCaptureStackTrace) Error.captureStackTrace(this, constructor) else Object.defineProperty(this, 'stack', { value: Error(message).stack }) Object.defineProperties(this, { name: { value: name }, message: { value: message } }) } export class BadRequestError extends Error { constructor () { super(); setup.apply(this, arguments) } } export class UnauthorizedError extends Error { constructor () { super(); setup.apply(this, arguments) } } export class ForbiddenError extends Error { constructor () { super(); setup.apply(this, arguments) } } export class NotFoundError extends Error { constructor () { super(); setup.apply(this, arguments) } }