index.js 915 B

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