mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
lib: add cause to DOMException
PR-URL: https://github.com/nodejs/node/pull/44703 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
parent
f594cc85b7
commit
00e5617533
@ -49,12 +49,31 @@ const disusedNamesSet = new SafeSet()
|
||||
.add('ValidationError');
|
||||
|
||||
class DOMException {
|
||||
constructor(message = '', name = 'Error') {
|
||||
constructor(message = '', options = 'Error') {
|
||||
ErrorCaptureStackTrace(this);
|
||||
internalsMap.set(this, {
|
||||
message: `${message}`,
|
||||
name: `${name}`
|
||||
});
|
||||
|
||||
if (options && typeof options === 'object') {
|
||||
const { name } = options;
|
||||
internalsMap.set(this, {
|
||||
message: `${message}`,
|
||||
name: `${name}`
|
||||
});
|
||||
|
||||
if ('cause' in options) {
|
||||
ObjectDefineProperty(this, 'cause', {
|
||||
__proto__: null,
|
||||
value: options.cause,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
enumerable: false,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
internalsMap.set(this, {
|
||||
message: `${message}`,
|
||||
name: `${options}`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
get name() {
|
||||
|
33
test/parallel/test-domexception-cause.js
Normal file
33
test/parallel/test-domexception-cause.js
Normal file
@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const { strictEqual, deepStrictEqual } = require('assert');
|
||||
|
||||
{
|
||||
const domException = new DOMException('no cause', 'abc');
|
||||
strictEqual(domException.name, 'abc');
|
||||
strictEqual('cause' in domException, false);
|
||||
strictEqual(domException.cause, undefined);
|
||||
}
|
||||
|
||||
{
|
||||
const domException = new DOMException('with undefined cause', { name: 'abc', cause: undefined });
|
||||
strictEqual(domException.name, 'abc');
|
||||
strictEqual('cause' in domException, true);
|
||||
strictEqual(domException.cause, undefined);
|
||||
}
|
||||
|
||||
{
|
||||
const domException = new DOMException('with string cause', { name: 'abc', cause: 'foo' });
|
||||
strictEqual(domException.name, 'abc');
|
||||
strictEqual('cause' in domException, true);
|
||||
strictEqual(domException.cause, 'foo');
|
||||
}
|
||||
|
||||
{
|
||||
const object = { reason: 'foo' };
|
||||
const domException = new DOMException('with object cause', { name: 'abc', cause: object });
|
||||
strictEqual(domException.name, 'abc');
|
||||
strictEqual('cause' in domException, true);
|
||||
deepStrictEqual(domException.cause, object);
|
||||
}
|
Loading…
Reference in New Issue
Block a user