Skip to content

Commit

Permalink
Assign .cause if not handled by super() call
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Dec 7, 2023
1 parent b27692d commit 7c7f86c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ exports.Boom = class Boom extends Error {
super(message ?? internals.codes.get(statusCode) ?? 'Unknown', { cause });
Error.captureStackTrace(this, ctor); // Filter the stack to our external API

if (cause !== undefined) {
this.cause ??= cause; // Explicitly assign cause to work with old runtimes
}

internals.apply(this, data, statusCode, headers);
}

Expand Down
21 changes: 21 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ describe('Boom', () => {
expect(err.output.payload.error).to.equal('Unknown');
});

it('assigns a .cause property if Error does not support it', (flags) => {

const proto = Object.getPrototypeOf(Boom.Boom);
Object.setPrototypeOf(Boom.Boom, class extends Error {

constructor(message, _options) {

super(message);
}
});

flags.onCleanup = () => {

Object.setPrototypeOf(Boom.Boom, proto);
};

const err = new Boom.Boom('fail', { cause: 0 });
expect(err.cause).to.exist();
expect(err.cause).to.equal(0);
});

describe('instanceof', () => {

it('identifies a boom object', () => {
Expand Down

0 comments on commit 7c7f86c

Please sign in to comment.