Skip to content

Commit

Permalink
Set falsy cause:es correctly on ErrorWithCause
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed May 31, 2022
1 parent d1c1fbc commit 91487c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions test/error.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 91487c2

Please sign in to comment.