Skip to content

Commit

Permalink
lib: add cause to DOMException
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Sep 17, 2022
1 parent 5a40d2e commit c871867
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/internal/per_context/domexception.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,23 @@ 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, cause } = options;
internalsMap.set(this, {
message: `${message}`,
name: `${name}`
});

ObjectDefineProperty(this, 'cause', { __proto__: null, value: cause });
} 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);
}

0 comments on commit c871867

Please sign in to comment.