UNPKG

3.24 kBJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var global = require('../internals/global');
4var getBuiltIn = require('../internals/get-built-in');
5var createPropertyDescriptor = require('../internals/create-property-descriptor');
6var defineProperty = require('../internals/object-define-property').f;
7var hasOwn = require('../internals/has-own-property');
8var anInstance = require('../internals/an-instance');
9var inheritIfRequired = require('../internals/inherit-if-required');
10var normalizeStringArgument = require('../internals/normalize-string-argument');
11var DOMExceptionConstants = require('../internals/dom-exception-constants');
12var clearErrorStack = require('../internals/error-stack-clear');
13var DESCRIPTORS = require('../internals/descriptors');
14var IS_PURE = require('../internals/is-pure');
15
16var DOM_EXCEPTION = 'DOMException';
17var Error = getBuiltIn('Error');
18var NativeDOMException = getBuiltIn(DOM_EXCEPTION);
19
20var $DOMException = function DOMException() {
21 anInstance(this, DOMExceptionPrototype);
22 var argumentsLength = arguments.length;
23 var message = normalizeStringArgument(argumentsLength < 1 ? undefined : arguments[0]);
24 var name = normalizeStringArgument(argumentsLength < 2 ? undefined : arguments[1], 'Error');
25 var that = new NativeDOMException(message, name);
26 var error = new Error(message);
27 error.name = DOM_EXCEPTION;
28 defineProperty(that, 'stack', createPropertyDescriptor(1, clearErrorStack(error.stack, 1)));
29 inheritIfRequired(that, this, $DOMException);
30 return that;
31};
32
33var DOMExceptionPrototype = $DOMException.prototype = NativeDOMException.prototype;
34
35var ERROR_HAS_STACK = 'stack' in new Error(DOM_EXCEPTION);
36var DOM_EXCEPTION_HAS_STACK = 'stack' in new NativeDOMException(1, 2);
37
38// eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
39var descriptor = NativeDOMException && DESCRIPTORS && Object.getOwnPropertyDescriptor(global, DOM_EXCEPTION);
40
41// Bun ~ 0.1.1 DOMException have incorrect descriptor and we can't redefine it
42// https://github.com/Jarred-Sumner/bun/issues/399
43var BUGGY_DESCRIPTOR = !!descriptor && !(descriptor.writable && descriptor.configurable);
44
45var FORCED_CONSTRUCTOR = ERROR_HAS_STACK && !BUGGY_DESCRIPTOR && !DOM_EXCEPTION_HAS_STACK;
46
47// `DOMException` constructor patch for `.stack` where it's required
48// https://webidl.spec.whatwg.org/#es-DOMException-specialness
49$({ global: true, constructor: true, forced: IS_PURE || FORCED_CONSTRUCTOR }, { // TODO: fix export logic
50 DOMException: FORCED_CONSTRUCTOR ? $DOMException : NativeDOMException
51});
52
53var PolyfilledDOMException = getBuiltIn(DOM_EXCEPTION);
54var PolyfilledDOMExceptionPrototype = PolyfilledDOMException.prototype;
55
56if (PolyfilledDOMExceptionPrototype.constructor !== PolyfilledDOMException) {
57 if (!IS_PURE) {
58 defineProperty(PolyfilledDOMExceptionPrototype, 'constructor', createPropertyDescriptor(1, PolyfilledDOMException));
59 }
60
61 for (var key in DOMExceptionConstants) if (hasOwn(DOMExceptionConstants, key)) {
62 var constant = DOMExceptionConstants[key];
63 var constantName = constant.s;
64 if (!hasOwn(PolyfilledDOMException, constantName)) {
65 defineProperty(PolyfilledDOMException, constantName, createPropertyDescriptor(6, constant.c));
66 }
67 }
68}