|
| 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