Skip to content

Commit df06f28

Browse files
committed
lib: fix DOMException subclass support
1 parent fb614c4 commit df06f28

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

lib/internal/per_context/domexception.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class DOMException {
7575
// internal slot.
7676
// eslint-disable-next-line no-restricted-syntax
7777
const self = new Error();
78-
ObjectSetPrototypeOf(self, DOMExceptionPrototype);
78+
// Use `new.target.prototype` to support DOMException subclasses.
79+
ObjectSetPrototypeOf(self, new.target.prototype);
7980
self[transfer_mode_private_symbol] = kCloneable;
8081

8182
if (options && typeof options === 'object') {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
6+
class MyDOMException extends DOMException {
7+
ownProp;
8+
#reason;
9+
10+
constructor() {
11+
super('my message', 'NotFoundError');
12+
this.ownProp = 'bar';
13+
this.#reason = 'hello';
14+
}
15+
16+
get reason () {
17+
return this.#reason;
18+
}
19+
}
20+
21+
const myException = new MyDOMException();
22+
// Verifies the prototype chain
23+
assert(myException instanceof MyDOMException);
24+
assert(myException instanceof DOMException);
25+
assert(myException instanceof Error);
26+
// Verifies [[ErrorData]]
27+
assert(Error.isError(myException));
28+
29+
// Verifies subclass properties
30+
assert(Object.hasOwn(myException, 'ownProp'));
31+
assert(!Object.hasOwn(myException, 'reason'));
32+
assert.strictEqual(myException.reason, 'hello');
33+
34+
// Verifies error properties
35+
assert.strictEqual(myException.name, 'NotFoundError');
36+
assert.strictEqual(myException.code, 8);
37+
assert.strictEqual(myException.message, 'my message');
38+
assert.strictEqual(typeof myException.stack, 'string');
39+
40+
// Verify structuredClone only copies known error properties.
41+
const cloned = structuredClone(myException);
42+
assert(!(cloned instanceof MyDOMException));
43+
assert(cloned instanceof DOMException);
44+
assert(cloned instanceof Error);
45+
assert(Error.isError(cloned));
46+
47+
// Verify custom properties
48+
assert(!Object.hasOwn(cloned, 'ownProp'));
49+
assert.strictEqual(cloned.reason, undefined);
50+
51+
// Verify cloned error properties
52+
assert.strictEqual(cloned.name, 'NotFoundError');
53+
assert.strictEqual(cloned.code, 8);
54+
assert.strictEqual(cloned.message, 'my message');
55+
assert.strictEqual(cloned.stack, myException.stack);

0 commit comments

Comments
 (0)