diff --git a/index.js b/index.js index 3280f14..9c20a2c 100644 --- a/index.js +++ b/index.js @@ -6,14 +6,14 @@ class ErrorWithCause extends Error { // linemod-prefix-with: export * @param {string} message * @param {{ cause?: T }} [options] */ - constructor (message, { cause } = {}) { + constructor (message, options) { super(message); /** @type {string} */ this.name = ErrorWithCause.name; - if (cause) { - /** @type {T} */ - this.cause = cause; + if (options && Object.prototype.hasOwnProperty.call(options, 'cause')) { + /** @type {T|undefined} */ + this.cause = options.cause; } /** @type {string} */ this.message = message; diff --git a/test/error.spec.js b/test/error.spec.js index 1f8028c..4d65981 100644 --- a/test/error.spec.js +++ b/test/error.spec.js @@ -37,4 +37,17 @@ describe('ErrorWithCause', () => { const err = new ErrorWithCause('Foo'); err.should.have.property('stack').that.is.a('string').which.startsWith('ErrorWithCause: Foo\n'); }); + + it('should set cause property when given undefined cause', () => { + (new ErrorWithCause('Foo', { cause: undefined })).should.have.property('cause', undefined); + }); + + it('should set cause property when given null cause', () => { + // eslint-disable-next-line unicorn/no-null + (new ErrorWithCause('Foo', { cause: null })).should.have.property('cause', null); + }); + + it('should set cause property when given false cause', () => { + (new ErrorWithCause('Foo', { cause: false })).should.have.property('cause', false); + }); });