GeezerVison is showing github.com — lib: add cause to DOMException · nodejs/node@67eaa30
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/per_context/domexception.js +24-5Lines changed: 24 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,12 +49,31 @@ const disusedNamesSet = new SafeSet() | 49 | 49 | .add('ValidationError'); | 50 | 50 |
| 51 | 51 | class DOMException { | 52 | | - constructor(message = '', name = 'Error') { | | 52 | + constructor(message = '', options = 'Error') { | 53 | 53 | 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 | + } | 58 | 77 | } | 59 | 78 |
| 60 | 79 | get name() { | |
Collapse file Copy file name to clipboard +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
|