Skip to content

Commit 67eaa30

Browse files
flakey5danielleadams
authored andcommitted
lib: add cause to DOMException
PR-URL: #44703 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 780144c commit 67eaa30

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

lib/internal/per_context/domexception.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,31 @@ const disusedNamesSet = new SafeSet()
4949
.add('ValidationError');
5050

5151
class DOMException {
52-
constructor(message = '', name = 'Error') {
52+
constructor(message = '', options = 'Error') {
5353
ErrorCaptureStackTrace(this);
54-
internalsMap.set(this, {
55-
message: `${message}`,
56-
name: `${name}`
57-
});
54+
55+
if (options && typeof options === 'object') {
56+
const { name } = options;
57+
internalsMap.set(this, {
58+
message: `${message}`,
59+
name: `${name}`
60+
});
61+
62+
if ('cause' in options) {
63+
ObjectDefineProperty(this, 'cause', {
64+
__proto__: null,
65+
value: options.cause,
66+
configurable: true,
67+
writable: true,
68+
enumerable: false,
69+
});
70+
}
71+
} else {
72+
internalsMap.set(this, {
73+
message: `${message}`,
74+
name: `${options}`
75+
});
76+
}
5877
}
5978

6079
get name() {
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
require('../common');
4+
const { strictEqual, deepStrictEqual } = require('assert');
5+
6+
{
7+
const domException = new DOMException('no cause', 'abc');
8+
strictEqual(domException.name, 'abc');
9+
strictEqual('cause' in domException, false);
10+
strictEqual(domException.cause, undefined);
11+
}
12+
13+
{
14+
const domException = new DOMException('with undefined cause', { name: 'abc', cause: undefined });
15+
strictEqual(domException.name, 'abc');
16+
strictEqual('cause' in domException, true);
17+
strictEqual(domException.cause, undefined);
18+
}
19+
20+
{
21+
const domException = new DOMException('with string cause', { name: 'abc', cause: 'foo' });
22+
strictEqual(domException.name, 'abc');
23+
strictEqual('cause' in domException, true);
24+
strictEqual(domException.cause, 'foo');
25+
}
26+
27+
{
28+
const object = { reason: 'foo' };
29+
const domException = new DOMException('with object cause', { name: 'abc', cause: object });
30+
strictEqual(domException.name, 'abc');
31+
strictEqual('cause' in domException, true);
32+
deepStrictEqual(domException.cause, object);
33+
}

0 commit comments

Comments
 (0)