esnext.suppressed-error.constructor.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var globalThis = require('../internals/global-this');
  4. var isPrototypeOf = require('../internals/object-is-prototype-of');
  5. var getPrototypeOf = require('../internals/object-get-prototype-of');
  6. var setPrototypeOf = require('../internals/object-set-prototype-of');
  7. var copyConstructorProperties = require('../internals/copy-constructor-properties');
  8. var create = require('../internals/object-create');
  9. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  10. var createPropertyDescriptor = require('../internals/create-property-descriptor');
  11. var installErrorStack = require('../internals/error-stack-install');
  12. var normalizeStringArgument = require('../internals/normalize-string-argument');
  13. var wellKnownSymbol = require('../internals/well-known-symbol');
  14. var fails = require('../internals/fails');
  15. var IS_PURE = require('../internals/is-pure');
  16. var NativeSuppressedError = globalThis.SuppressedError;
  17. var TO_STRING_TAG = wellKnownSymbol('toStringTag');
  18. var $Error = Error;
  19. // https://github.com/oven-sh/bun/issues/9282
  20. var WRONG_ARITY = !!NativeSuppressedError && NativeSuppressedError.length !== 3;
  21. // https://github.com/oven-sh/bun/issues/9283
  22. var EXTRA_ARGS_SUPPORT = !!NativeSuppressedError && fails(function () {
  23. return new NativeSuppressedError(1, 2, 3, { cause: 4 }).cause === 4;
  24. });
  25. var PATCH = WRONG_ARITY || EXTRA_ARGS_SUPPORT;
  26. var $SuppressedError = function SuppressedError(error, suppressed, message) {
  27. var isInstance = isPrototypeOf(SuppressedErrorPrototype, this);
  28. var that;
  29. if (setPrototypeOf) {
  30. that = PATCH && (!isInstance || getPrototypeOf(this) === SuppressedErrorPrototype)
  31. ? new NativeSuppressedError()
  32. : setPrototypeOf(new $Error(), isInstance ? getPrototypeOf(this) : SuppressedErrorPrototype);
  33. } else {
  34. that = isInstance ? this : create(SuppressedErrorPrototype);
  35. createNonEnumerableProperty(that, TO_STRING_TAG, 'Error');
  36. }
  37. if (message !== undefined) createNonEnumerableProperty(that, 'message', normalizeStringArgument(message));
  38. installErrorStack(that, $SuppressedError, that.stack, 1);
  39. createNonEnumerableProperty(that, 'error', error);
  40. createNonEnumerableProperty(that, 'suppressed', suppressed);
  41. return that;
  42. };
  43. if (setPrototypeOf) setPrototypeOf($SuppressedError, $Error);
  44. else copyConstructorProperties($SuppressedError, $Error, { name: true });
  45. var SuppressedErrorPrototype = $SuppressedError.prototype = PATCH ? NativeSuppressedError.prototype : create($Error.prototype, {
  46. constructor: createPropertyDescriptor(1, $SuppressedError),
  47. message: createPropertyDescriptor(1, ''),
  48. name: createPropertyDescriptor(1, 'SuppressedError')
  49. });
  50. if (PATCH && !IS_PURE) SuppressedErrorPrototype.constructor = $SuppressedError;
  51. // `SuppressedError` constructor
  52. // https://github.com/tc39/proposal-explicit-resource-management
  53. $({ global: true, constructor: true, arity: 3, forced: PATCH }, {
  54. SuppressedError: $SuppressedError
  55. });