Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add cause to DOMException #44703

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions lib/internal/per_context/domexception.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
flakey5 marked this conversation as resolved.
Show resolved Hide resolved
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() {
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-domexception-cause.js
Original file line number Diff line number Diff line change
@@ -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);
}