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 b2102b1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 51 deletions.
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Creates a new `Boom` sub-classed `Error` object, where:
- `message` - the error message.
- `options` - and optional object where:
- `statusCode` - the HTTP status code. Defaults to `500`.
- `cause` - The error that caused the boom error.
- `cause` - the error that caused the boom error.
- `data` - additional error information, assigned to `this.data`.
- `headers` - an object containing any HTTP headers where each key is a header name and value is the header content.
- `ctor` - constructor reference used to crop the exception call stack output.
Expand Down
10 changes: 5 additions & 5 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 Expand Up @@ -407,7 +409,5 @@ exports.gatewayTimeout = internals.statusError(504, internals.serverError);

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

const res = internals.serverError(message, data);
res.push({ isDeveloperError: true });
return res;
return [...internals.serverError(message, data), { isDeveloperError: true }];
});
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 b2102b1

Please sign in to comment.