UNPKG

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