es.aggregate-error.constructor.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var isPrototypeOf = require('../internals/object-is-prototype-of');
  4. var getPrototypeOf = require('../internals/object-get-prototype-of');
  5. var setPrototypeOf = require('../internals/object-set-prototype-of');
  6. var copyConstructorProperties = require('../internals/copy-constructor-properties');
  7. var create = require('../internals/object-create');
  8. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  9. var createPropertyDescriptor = require('../internals/create-property-descriptor');
  10. var installErrorCause = require('../internals/install-error-cause');
  11. var installErrorStack = require('../internals/error-stack-install');
  12. var iterate = require('../internals/iterate');
  13. var normalizeStringArgument = require('../internals/normalize-string-argument');
  14. var wellKnownSymbol = require('../internals/well-known-symbol');
  15. var TO_STRING_TAG = wellKnownSymbol('toStringTag');
  16. var $Error = Error;
  17. var push = [].push;
  18. var $AggregateError = function AggregateError(errors, message /* , options */) {
  19. var isInstance = isPrototypeOf(AggregateErrorPrototype, this);
  20. var that;
  21. if (setPrototypeOf) {
  22. that = setPrototypeOf(new $Error(), isInstance ? getPrototypeOf(this) : AggregateErrorPrototype);
  23. } else {
  24. that = isInstance ? this : create(AggregateErrorPrototype);
  25. createNonEnumerableProperty(that, TO_STRING_TAG, 'Error');
  26. }
  27. if (message !== undefined) createNonEnumerableProperty(that, 'message', normalizeStringArgument(message));
  28. installErrorStack(that, $AggregateError, that.stack, 1);
  29. if (arguments.length > 2) installErrorCause(that, arguments[2]);
  30. var errorsArray = [];
  31. iterate(errors, push, { that: errorsArray });
  32. createNonEnumerableProperty(that, 'errors', errorsArray);
  33. return that;
  34. };
  35. if (setPrototypeOf) setPrototypeOf($AggregateError, $Error);
  36. else copyConstructorProperties($AggregateError, $Error, { name: true });
  37. var AggregateErrorPrototype = $AggregateError.prototype = create($Error.prototype, {
  38. constructor: createPropertyDescriptor(1, $AggregateError),
  39. message: createPropertyDescriptor(1, ''),
  40. name: createPropertyDescriptor(1, 'AggregateError')
  41. });
  42. // `AggregateError` constructor
  43. // https://tc39.es/ecma262/#sec-aggregate-error-constructor
  44. $({ global: true, constructor: true, arity: 2 }, {
  45. AggregateError: $AggregateError
  46. });