1'use strict'; 2var captureStackTrace = require('capture-stack-trace'); 3 4function inherits(ctor, superCtor) { 5 ctor.super_ = superCtor; 6 ctor.prototype = Object.create(superCtor.prototype, { 7 constructor: { 8 value: ctor, 9 enumerable: false, 10 writable: true, 11 configurable: true 12 } 13 }); 14} 15 16module.exports = function createErrorClass(className, setup) { 17 if (typeof className !== 'string') { 18 throw new TypeError('Expected className to be a string'); 19 } 20 21 if (/[^0-9a-zA-Z_$]/.test(className)) { 22 throw new Error('className contains invalid characters'); 23 } 24 25 setup = setup || function (message) { 26 this.message = message; 27 }; 28 29 var ErrorClass = function () { 30 Object.defineProperty(this, 'name', { 31 configurable: true, 32 value: className, 33 writable: true 34 }); 35 36 captureStackTrace(this, this.constructor); 37 38 setup.apply(this, arguments); 39 }; 40 41 inherits(ErrorClass, Error); 42 43 return ErrorClass; 44}; 45