Skip to content

Commit 91487c2

Browse files
committed
Set falsy cause:es correctly on ErrorWithCause
1 parent d1c1fbc commit 91487c2

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ class ErrorWithCause extends Error { // linemod-prefix-with: export
66
* @param {string} message
77
* @param {{ cause?: T }} [options]
88
*/
9-
constructor (message, { cause } = {}) {
9+
constructor (message, options) {
1010
super(message);
1111

1212
/** @type {string} */
1313
this.name = ErrorWithCause.name;
14-
if (cause) {
15-
/** @type {T} */
16-
this.cause = cause;
14+
if (options && Object.prototype.hasOwnProperty.call(options, 'cause')) {
15+
/** @type {T|undefined} */
16+
this.cause = options.cause;
1717
}
1818
/** @type {string} */
1919
this.message = message;

test/error.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,17 @@ describe('ErrorWithCause', () => {
3737
const err = new ErrorWithCause('Foo');
3838
err.should.have.property('stack').that.is.a('string').which.startsWith('ErrorWithCause: Foo\n');
3939
});
40+
41+
it('should set cause property when given undefined cause', () => {
42+
(new ErrorWithCause('Foo', { cause: undefined })).should.have.property('cause', undefined);
43+
});
44+
45+
it('should set cause property when given null cause', () => {
46+
// eslint-disable-next-line unicorn/no-null
47+
(new ErrorWithCause('Foo', { cause: null })).should.have.property('cause', null);
48+
});
49+
50+
it('should set cause property when given false cause', () => {
51+
(new ErrorWithCause('Foo', { cause: false })).should.have.property('cause', false);
52+
});
4053
});

0 commit comments

Comments
 (0)