Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Oct 31, 2024
1 parent e114fbd commit 4203898
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 47 deletions.
6 changes: 4 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ exports.Boom = class Boom extends Error {
}

static {
Object.defineProperty(this.prototype, 'name', { value: 'Boom', writable: true, configurable: true });
Object.defineProperty(this.prototype, 'isBoom', { value: true, writable: true, configurable: true });
Object.defineProperties(this.prototype, {
name: { value: 'Boom', writable: true, configurable: true },
isBoom: { value: true, writable: true, configurable: true }
});
}
};

Expand Down
70 changes: 25 additions & 45 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,17 @@ describe('Boom', () => {
});
});

const utilities = ['badRequest', 'unauthorized', 'forbidden', 'notFound', 'methodNotAllowed',
'notAcceptable', 'proxyAuthRequired', 'clientTimeout', 'conflict',
'resourceGone', 'lengthRequired', 'preconditionFailed', 'entityTooLarge',
'uriTooLong', 'unsupportedMediaType', 'rangeNotSatisfiable', 'expectationFailed',
'badData', 'preconditionRequired', 'tooManyRequests',

// 500s
'internal', 'notImplemented', 'badGateway', 'serverUnavailable',
'gatewayTimeout', 'badImplementation'
];

describe('badRequest()', () => {

it('returns a 400 error statusCode', () => {
Expand Down Expand Up @@ -1055,25 +1066,22 @@ describe('Boom', () => {
}
});
});
});

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

const helpers = ['badRequest', 'unauthorized', 'forbidden', 'notFound', 'methodNotAllowed',
'notAcceptable', 'proxyAuthRequired', 'clientTimeout', 'conflict',
'resourceGone', 'lengthRequired', 'preconditionFailed', 'entityTooLarge',
'uriTooLong', 'unsupportedMediaType', 'rangeNotSatisfiable', 'expectationFailed',
'badData', 'preconditionRequired', 'tooManyRequests',
const insideErr = new Error('inside');
const err = Boom.badImplementation('my message', insideErr);
expect(err.data).to.not.exist();
expect(err.cause).to.shallow.equal(insideErr);
});
});

// 500s
'internal', 'notImplemented', 'badGateway', 'serverUnavailable',
'gatewayTimeout', 'badImplementation'
];
describe('stack trace', () => {

it('should omit lib', () => {

for (const helper of helpers) {
const err = Boom[helper]();
for (const name of utilities) {
const err = Boom[name]();
expect(err.stack).to.not.match(/(\/|\\)lib(\/|\\)index\.js/);
}
});
Expand All @@ -1082,10 +1090,10 @@ describe('Boom', () => {

const captureStackTrace = Error.captureStackTrace;

for (const helper of helpers) {
for (const name of utilities) {
try {
Error.captureStackTrace = undefined;
var err = Boom[helper]();
var err = Boom[name]();
}
finally {
Error.captureStackTrace = captureStackTrace;
Expand All @@ -1098,35 +1106,7 @@ describe('Boom', () => {

describe('method with error object instead of message', () => {

[
'badRequest',
'unauthorized',
'forbidden',
'notFound',
'methodNotAllowed',
'notAcceptable',
'proxyAuthRequired',
'clientTimeout',
'conflict',
'resourceGone',
'lengthRequired',
'preconditionFailed',
'entityTooLarge',
'uriTooLong',
'unsupportedMediaType',
'rangeNotSatisfiable',
'expectationFailed',
'badData',
'preconditionRequired',
'tooManyRequests',
'internal',
'notImplemented',
'badGateway',
'serverUnavailable',
'gatewayTimeout',
'badImplementation'
].forEach((name) => {

for (const name of utilities) {
it(`uses stringified error as message`, () => {

const error = new Error('An example mongoose validation error');
Expand All @@ -1135,7 +1115,7 @@ describe('Boom', () => {
expect(err.cause).to.not.exist();
expect(err.message).to.equal(error.toString());
});
});
}
});

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

0 comments on commit 4203898

Please sign in to comment.