Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for Boom.someMethod(err), closes #138 #139

Merged
merged 3 commits into from Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/index.js
Expand Up @@ -85,6 +85,13 @@ exports.create = function (statusCode, message, data) {

internals.create = function (statusCode, message, data, ctor) {

if (message instanceof Error) {
if (data) {
message.data = data;
}
return exports.wrap(message, statusCode);
}

const error = new Error(message ? message : undefined); // Avoids settings null message
Error.captureStackTrace(error, ctor); // Filter the stack to our external API
error.data = data || null;
Expand Down
42 changes: 42 additions & 0 deletions test/index.js
Expand Up @@ -726,3 +726,45 @@ describe('stack trace', () => {
done();
});
});

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',

// 500s
'internal', 'notImplemented', 'badGateway', 'serverUnavailable',
'gatewayTimeout', 'badImplementation'
].forEach((name) => {

it(`should allow \`Boom${name}(err)\` and preserve the error`, (done) => {

const error = new Error('An example mongoose validation error');
error.name = 'ValidationError';
const err = Boom[name](error);
expect(err.name).to.equal('ValidationError');
expect(err.message).to.equal('An example mongoose validation error');
done();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra whitespace.

});

// exclude unauthorized
if (name !== 'unauthorized') {

it(`should allow \`Boom.${name}(err, data)\` and preserve the data`, (done) => {

const error = new Error();
const err = Boom[name](error, { foo: 'bar' });
expect(err.data).to.equal({ foo: 'bar' });
done();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra whitespace.

});

}

});

});