Skip to content

Commit 8a99eed

Browse files
XadillaXBethGriggs
authored andcommitted
lib: modify DOMException to pass WPT
PR-URL: #41517 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9c73bd5 commit 8a99eed

File tree

10 files changed

+468
-4
lines changed

10 files changed

+468
-4
lines changed

lib/internal/per_context/domexception.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use strict';
22

33
const {
4-
Error,
4+
ErrorCaptureStackTrace,
5+
ErrorPrototype,
56
ObjectDefineProperties,
67
ObjectDefineProperty,
8+
ObjectSetPrototypeOf,
79
SafeWeakMap,
810
SafeMap,
11+
SafeSet,
912
SymbolToStringTag,
1013
TypeError,
1114
} = primordials;
@@ -33,6 +36,7 @@ function throwInvalidThisError(Base, type) {
3336
throw err;
3437
}
3538

39+
let disusedNamesSet;
3640
let internalsMap;
3741
let nameToCodeMap;
3842
let isInitialized = false;
@@ -49,13 +53,21 @@ function ensureInitialized() {
4953
forEachCode((name, codeName, value) => {
5054
nameToCodeMap.set(name, value);
5155
});
56+
57+
// These were removed from the error names table.
58+
// See https://github.com/heycam/webidl/pull/946.
59+
disusedNamesSet = new SafeSet()
60+
.add('DOMStringSizeError')
61+
.add('NoDataAllowedError')
62+
.add('ValidationError');
63+
5264
isInitialized = true;
5365
}
5466

55-
class DOMException extends Error {
67+
class DOMException {
5668
constructor(message = '', name = 'Error') {
5769
ensureInitialized();
58-
super();
70+
ErrorCaptureStackTrace(this);
5971
internalsMap.set(this, {
6072
message: `${message}`,
6173
name: `${name}`
@@ -86,11 +98,17 @@ class DOMException extends Error {
8698
if (internals === undefined) {
8799
throwInvalidThisError(TypeError, 'DOMException');
88100
}
101+
102+
if (disusedNamesSet.has(internals.name)) {
103+
return 0;
104+
}
105+
89106
const code = nameToCodeMap.get(internals.name);
90107
return code === undefined ? 0 : code;
91108
}
92109
}
93110

111+
ObjectSetPrototypeOf(DOMException.prototype, ErrorPrototype);
94112
ObjectDefineProperties(DOMException.prototype, {
95113
[SymbolToStringTag]: { configurable: true, value: 'DOMException' },
96114
name: { enumerable: true, configurable: true },

test/fixtures/wpt/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Last update:
2727
- url: https://github.com/web-platform-tests/wpt/tree/77d54aa9e0/url
2828
- user-timing: https://github.com/web-platform-tests/wpt/tree/df24fb604e/user-timing
2929
- WebCryptoAPI: https://github.com/web-platform-tests/wpt/tree/cdd0f03df4/WebCryptoAPI
30+
- webidl/ecmascript-binding/es-exceptions: https://github.com/web-platform-tests/wpt/tree/a370aad338/webidl/ecmascript-binding/es-exceptions
3031

3132
[Web Platform Tests]: https://github.com/web-platform-tests/wpt
3233
[`git node wpt`]: https://github.com/nodejs/node-core-utils/blob/main/docs/git-node.md#git-node-wpt

test/fixtures/wpt/versions.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,9 @@
6666
"WebCryptoAPI": {
6767
"commit": "cdd0f03df41b222aed098fbbb11c6a3cc500a86b",
6868
"path": "WebCryptoAPI"
69+
},
70+
"webidl/ecmascript-binding/es-exceptions": {
71+
"commit": "a370aad338d6ed743abb4d2c6ae84a7f1058558c",
72+
"path": "webidl/ecmascript-binding/es-exceptions"
6973
}
70-
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
test(function() {
4+
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=27732
5+
var constants = [
6+
"INDEX_SIZE_ERR",
7+
"DOMSTRING_SIZE_ERR",
8+
"HIERARCHY_REQUEST_ERR",
9+
"WRONG_DOCUMENT_ERR",
10+
"INVALID_CHARACTER_ERR",
11+
"NO_DATA_ALLOWED_ERR",
12+
"NO_MODIFICATION_ALLOWED_ERR",
13+
"NOT_FOUND_ERR",
14+
"NOT_SUPPORTED_ERR",
15+
"INUSE_ATTRIBUTE_ERR",
16+
"INVALID_STATE_ERR",
17+
"SYNTAX_ERR",
18+
"INVALID_MODIFICATION_ERR",
19+
"NAMESPACE_ERR",
20+
"INVALID_ACCESS_ERR",
21+
"VALIDATION_ERR",
22+
"TYPE_MISMATCH_ERR",
23+
"SECURITY_ERR",
24+
"NETWORK_ERR",
25+
"ABORT_ERR",
26+
"URL_MISMATCH_ERR",
27+
"QUOTA_EXCEEDED_ERR",
28+
"TIMEOUT_ERR",
29+
"INVALID_NODE_TYPE_ERR",
30+
"DATA_CLONE_ERR"
31+
]
32+
var objects = [
33+
[DOMException, "DOMException constructor object"],
34+
[DOMException.prototype, "DOMException prototype object"]
35+
]
36+
constants.forEach(function(name, i) {
37+
objects.forEach(function(o) {
38+
var object = o[0], description = o[1];
39+
test(function() {
40+
assert_equals(object[name], i + 1, name)
41+
assert_own_property(object, name)
42+
var pd = Object.getOwnPropertyDescriptor(object, name)
43+
assert_false("get" in pd, "get")
44+
assert_false("set" in pd, "set")
45+
assert_false(pd.writable, "writable")
46+
assert_true(pd.enumerable, "enumerable")
47+
assert_false(pd.configurable, "configurable")
48+
}, "Constant " + name + " on " + description)
49+
})
50+
})
51+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
test(function() {
2+
assert_own_property(self, "DOMException", "property of global");
3+
4+
var desc = Object.getOwnPropertyDescriptor(self, "DOMException");
5+
assert_false("get" in desc, "get");
6+
assert_false("set" in desc, "set");
7+
assert_true(desc.writable, "writable");
8+
assert_false(desc.enumerable, "enumerable");
9+
assert_true(desc.configurable, "configurable");
10+
}, "existence and property descriptor of DOMException");
11+
12+
test(function() {
13+
assert_own_property(self.DOMException, "prototype", "prototype property");
14+
15+
var desc = Object.getOwnPropertyDescriptor(self.DOMException, "prototype");
16+
assert_false("get" in desc, "get");
17+
assert_false("set" in desc, "set");
18+
assert_false(desc.writable, "writable");
19+
assert_false(desc.enumerable, "enumerable");
20+
assert_false(desc.configurable, "configurable");
21+
}, "existence and property descriptor of DOMException.prototype");
22+
23+
test(function() {
24+
assert_own_property(self.DOMException.prototype, "constructor", "property of prototype");
25+
var desc = Object.getOwnPropertyDescriptor(self.DOMException.prototype, "constructor");
26+
assert_false("get" in desc, "get");
27+
assert_false("set" in desc, "set");
28+
assert_true(desc.writable, "writable");
29+
assert_false(desc.enumerable, "enumerable");
30+
assert_true(desc.configurable, "configurable");
31+
assert_equals(self.DOMException.prototype.constructor, self.DOMException, "equality with actual constructor");
32+
}, "existence and property descriptor of DOMException.prototype.constructor");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
'use strict';
2+
3+
test(function() {
4+
var ex = new DOMException();
5+
assert_equals(ex.name, "Error",
6+
"Not passing a name should end up with 'Error' as the name");
7+
assert_equals(ex.message, "",
8+
"Not passing a message should end up with empty string as the message");
9+
}, 'new DOMException()');
10+
11+
test(function() {
12+
var ex = new DOMException();
13+
assert_false(ex.hasOwnProperty("name"),
14+
"The name property should be inherited");
15+
assert_false(ex.hasOwnProperty("message"),
16+
"The message property should be inherited");
17+
}, 'new DOMException(): inherited-ness');
18+
19+
test(function() {
20+
var ex = new DOMException(null);
21+
assert_equals(ex.name, "Error",
22+
"Not passing a name should end up with 'Error' as the name");
23+
assert_equals(ex.message, "null",
24+
"Passing null as message should end up with stringified 'null' as the message");
25+
}, 'new DOMException(null)');
26+
27+
test(function() {
28+
var ex = new DOMException(undefined);
29+
assert_equals(ex.name, "Error",
30+
"Not passing a name should end up with 'Error' as the name");
31+
assert_equals(ex.message, "",
32+
"Not passing a message should end up with empty string as the message");
33+
}, 'new DOMException(undefined)');
34+
35+
test(function() {
36+
var ex = new DOMException(undefined);
37+
assert_false(ex.hasOwnProperty("name"),
38+
"The name property should be inherited");
39+
assert_false(ex.hasOwnProperty("message"),
40+
"The message property should be inherited");
41+
}, 'new DOMException(undefined): inherited-ness');
42+
43+
test(function() {
44+
var ex = new DOMException("foo");
45+
assert_equals(ex.name, "Error",
46+
"Not passing a name should still end up with 'Error' as the name");
47+
assert_equals(ex.message, "foo", "Should be using passed-in message");
48+
}, 'new DOMException("foo")');
49+
50+
test(function() {
51+
var ex = new DOMException("foo");
52+
assert_false(ex.hasOwnProperty("name"),
53+
"The name property should be inherited");
54+
assert_false(ex.hasOwnProperty("message"),
55+
"The message property should be inherited");
56+
}, 'new DOMException("foo"): inherited-ness');
57+
58+
test(function() {
59+
var ex = new DOMException("bar", undefined);
60+
assert_equals(ex.name, "Error",
61+
"Passing undefined for name should end up with 'Error' as the name");
62+
assert_equals(ex.message, "bar", "Should still be using passed-in message");
63+
}, 'new DOMException("bar", undefined)');
64+
65+
test(function() {
66+
var ex = new DOMException("bar", "NotSupportedError");
67+
assert_equals(ex.name, "NotSupportedError", "Should be using the passed-in name");
68+
assert_equals(ex.message, "bar", "Should still be using passed-in message");
69+
assert_equals(ex.code, DOMException.NOT_SUPPORTED_ERR,
70+
"Should have the right exception code");
71+
}, 'new DOMException("bar", "NotSupportedError")');
72+
73+
test(function() {
74+
var ex = new DOMException("bar", "NotSupportedError");
75+
assert_false(ex.hasOwnProperty("name"),
76+
"The name property should be inherited");
77+
assert_false(ex.hasOwnProperty("message"),
78+
"The message property should be inherited");
79+
}, 'new DOMException("bar", "NotSupportedError"): inherited-ness');
80+
81+
test(function() {
82+
var ex = new DOMException("bar", "foo");
83+
assert_equals(ex.name, "foo", "Should be using the passed-in name");
84+
assert_equals(ex.message, "bar", "Should still be using passed-in message");
85+
assert_equals(ex.code, 0,
86+
"Should have 0 for code for a name not in the exception names table");
87+
}, 'new DOMException("bar", "foo")');
88+
89+
[
90+
{name: "IndexSizeError", code: 1},
91+
{name: "HierarchyRequestError", code: 3},
92+
{name: "WrongDocumentError", code: 4},
93+
{name: "InvalidCharacterError", code: 5},
94+
{name: "NoModificationAllowedError", code: 7},
95+
{name: "NotFoundError", code: 8},
96+
{name: "NotSupportedError", code: 9},
97+
{name: "InUseAttributeError", code: 10},
98+
{name: "InvalidStateError", code: 11},
99+
{name: "SyntaxError", code: 12},
100+
{name: "InvalidModificationError", code: 13},
101+
{name: "NamespaceError", code: 14},
102+
{name: "InvalidAccessError", code: 15},
103+
{name: "TypeMismatchError", code: 17},
104+
{name: "SecurityError", code: 18},
105+
{name: "NetworkError", code: 19},
106+
{name: "AbortError", code: 20},
107+
{name: "URLMismatchError", code: 21},
108+
{name: "QuotaExceededError", code: 22},
109+
{name: "TimeoutError", code: 23},
110+
{name: "InvalidNodeTypeError", code: 24},
111+
{name: "DataCloneError", code: 25},
112+
113+
// These were removed from the error names table.
114+
// See https://github.com/heycam/webidl/pull/946.
115+
{name: "DOMStringSizeError", code: 0},
116+
{name: "NoDataAllowedError", code: 0},
117+
{name: "ValidationError", code: 0},
118+
119+
// The error names which don't have legacy code values.
120+
{name: "EncodingError", code: 0},
121+
{name: "NotReadableError", code: 0},
122+
{name: "UnknownError", code: 0},
123+
{name: "ConstraintError", code: 0},
124+
{name: "DataError", code: 0},
125+
{name: "TransactionInactiveError", code: 0},
126+
{name: "ReadOnlyError", code: 0},
127+
{name: "VersionError", code: 0},
128+
{name: "OperationError", code: 0},
129+
{name: "NotAllowedError", code: 0}
130+
].forEach(function(test_case) {
131+
test(function() {
132+
var ex = new DOMException("msg", test_case.name);
133+
assert_equals(ex.name, test_case.name,
134+
"Should be using the passed-in name");
135+
assert_equals(ex.message, "msg",
136+
"Should be using the passed-in message");
137+
assert_equals(ex.code, test_case.code,
138+
"Should have matching legacy code from error names table");
139+
},'new DOMexception("msg", "' + test_case.name + '")');
140+
});

0 commit comments

Comments
 (0)