Skip to content

Commit

Permalink
Allow to boomify non-Error errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Dec 4, 2023
1 parent 19e5e2a commit 9600c08
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ exports.Boom = class extends Error {
}
else {
this.output.payload.message = this.message;
if (this.cause?.message) {
this.output.payload.message = (this.message === this.output.payload.error) ? this.cause.message : this.message + ': ' + this.cause.message;
if (this.cause) {
const message = this.cause.message ?? this.cause;
this.output.payload.message = (this.message === this.output.payload.error) ? message : this.message + ': ' + message;
}
}
}
Expand Down Expand Up @@ -148,11 +149,9 @@ exports.isBoom = function (err, statusCode) {

exports.boomify = function (err, options = {}) {

Hoek.assert(err instanceof Error, 'Cannot boomify non-Error object');

const { override, data, statusCode, message } = options;

if (!err.isBoom) {
if (!err?.isBoom) {
return new exports.Boom(message, { statusCode, cause: err, data });
}

Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ describe('Boom', () => {
expect(boom.output.payload.message).to.equal('Override message: Missing data');
expect(boom.output.statusCode).to.equal(599);
});

it('handles non-Error errors', () => {

const boom = Boom.boomify(123, { message: 'Hello', statusCode: 400 });

expect(boom.cause).to.equal(123);
expect(boom.output.payload.message).to.equal('Hello: 123');
expect(boom.output.statusCode).to.equal(400);
});
});

describe('create()', () => {
Expand Down

0 comments on commit 9600c08

Please sign in to comment.