Skip to content

Commit

Permalink
Fix notImplemented and more setting Error cause
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Oct 31, 2024
1 parent e4ba24d commit e114fbd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,20 @@ exports.illegal = internals.statusError(451);

// 5xx Server Errors

internals.serverError = function (message, data) {

const isDataNonBoomError = data instanceof Error && !exports.isBoom(data);

return [message, isDataNonBoomError ? { cause: data } : { data }];
};


exports.internal = internals.statusError(500, (message, data, statusCode = 500) => {

const res = internals.serverError(message, data);
if (statusCode !== 500) {
res[1].statusCode = statusCode;
const [, options] = res;
options.statusCode = statusCode;
}

return res;
Expand All @@ -402,11 +411,3 @@ exports.badImplementation = internals.statusError(500, (message, data) => {
res.push({ isDeveloperError: true });
return res;
});


internals.serverError = function (message, data) {

const isDataNonBoomError = data instanceof Error && !exports.isBoom(data);

return [message, isDataNonBoomError ? { cause: data } : { data }];
};
32 changes: 32 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,14 @@ describe('Boom', () => {
expect(Boom.internal('my message', { my: 'data' }).data.my).to.equal('data');
});

it('uses data with Error as cause', () => {

const insideErr = new Error('inside');
const err = Boom.internal('my message', insideErr);
expect(err.data).to.not.exist();
expect(err.cause).to.shallow.equal(insideErr);
});

it('returns an error with composite message', () => {

const x = {};
Expand Down Expand Up @@ -949,6 +957,14 @@ describe('Boom', () => {

expect(Boom.notImplemented('my message').message).to.equal('my message');
});

it('uses data with Error as cause', () => {

const insideErr = new Error('inside');
const err = Boom.notImplemented('my message', insideErr);
expect(err.data).to.not.exist();
expect(err.cause).to.shallow.equal(insideErr);
});
});

describe('badGateway()', () => {
Expand All @@ -970,6 +986,14 @@ describe('Boom', () => {
expect(boom.output.statusCode).to.equal(502);
expect(boom.data).to.equal(upstream);
});

it('uses data with Error as cause', () => {

const insideErr = new Error('inside');
const err = Boom.badGateway('my message', insideErr);
expect(err.data).to.not.exist();
expect(err.cause).to.shallow.equal(insideErr);
});
});

describe('gatewayTimeout()', () => {
Expand All @@ -983,6 +1007,14 @@ describe('Boom', () => {

expect(Boom.gatewayTimeout('my message').message).to.equal('my message');
});

it('uses data with Error as cause', () => {

const insideErr = new Error('inside');
const err = Boom.gatewayTimeout('my message', insideErr);
expect(err.data).to.not.exist();
expect(err.cause).to.shallow.equal(insideErr);
});
});

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

0 comments on commit e114fbd

Please sign in to comment.