esnext.suppressed-error.constructor.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 installErrorStack = require('../internals/error-stack-install');
  11. var normalizeStringArgument = require('../internals/normalize-string-argument');
  12. var wellKnownSymbol = require('../internals/well-known-symbol');
  13. var TO_STRING_TAG = wellKnownSymbol('toStringTag');
  14. var $Error = Error;
  15. var $SuppressedError = function SuppressedError(error, suppressed, message) {
  16. var isInstance = isPrototypeOf(SuppressedErrorPrototype, this);
  17. var that;
  18. if (setPrototypeOf) {
  19. that = setPrototypeOf(new $Error(), isInstance ? getPrototypeOf(this) : SuppressedErrorPrototype);
  20. } else {
  21. that = isInstance ? this : create(SuppressedErrorPrototype);
  22. createNonEnumerableProperty(that, TO_STRING_TAG, 'Error');
  23. }
  24. if (message !== undefined) createNonEnumerableProperty(that, 'message', normalizeStringArgument(message));
  25. installErrorStack(that, $SuppressedError, that.stack, 1);
  26. createNonEnumerableProperty(that, 'error', error);
  27. createNonEnumerableProperty(that, 'suppressed', suppressed);
  28. return that;
  29. };
  30. if (setPrototypeOf) setPrototypeOf($SuppressedError, $Error);
  31. else copyConstructorProperties($SuppressedError, $Error, { name: true });
  32. var SuppressedErrorPrototype = $SuppressedError.prototype = create($Error.prototype, {
  33. constructor: createPropertyDescriptor(1, $SuppressedError),
  34. message: createPropertyDescriptor(1, ''),
  35. name: createPropertyDescriptor(1, 'SuppressedError')
  36. });
  37. // `SuppressedError` constructor
  38. // https://github.com/tc39/proposal-explicit-resource-management
  39. $({ global: true, constructor: true, arity: 3 }, {
  40. SuppressedError: $SuppressedError
  41. });