Skip to content

Commit

Permalink
Merge pull request #68 from hapijs/capture-stack
Browse files Browse the repository at this point in the history
Call captureStackTrace to filter boom from traces
  • Loading branch information
arb committed Sep 22, 2015
2 parents a0874d8 + 0a137e6 commit 62f51e6
Showing 1 changed file with 39 additions and 28 deletions.
67 changes: 39 additions & 28 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var Hoek = require('hoek');

var internals = {};


exports.wrap = function (error, statusCode, message) {

Hoek.assert(error instanceof Error, 'Cannot wrap non-Error object');
Expand All @@ -18,13 +17,18 @@ exports.wrap = function (error, statusCode, message) {

exports.create = function (statusCode, message, data) {

return internals.create(statusCode, message, data, exports.create);
};

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

var 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;
internals.initialize(error, statusCode);
return error;
};


internals.initialize = function (error, statusCode, message) {

var numberCode = parseInt(statusCode, 10);
Expand Down Expand Up @@ -78,13 +82,13 @@ internals.reformat = function () {

exports.badRequest = function (message, data) {

return exports.create(400, message, data);
return internals.create(400, message, data, exports.badRequest);
};


exports.unauthorized = function (message, scheme, attributes) { // Or function (message, wwwAuthenticate[])

var err = exports.create(401, message);
var err = internals.create(401, message, undefined, exports.unauthorized);

if (!scheme) {
return err;
Expand Down Expand Up @@ -156,112 +160,119 @@ exports.unauthorized = function (message, scheme, attributes) { // Or f

exports.forbidden = function (message, data) {

return exports.create(403, message, data);
return internals.create(403, message, data, exports.forbidden);
};


exports.notFound = function (message, data) {

return exports.create(404, message, data);
return internals.create(404, message, data, exports.notFound);
};


exports.methodNotAllowed = function (message, data) {

return exports.create(405, message, data);
return internals.create(405, message, data, exports.methodNotAllowed);
};


exports.notAcceptable = function (message, data) {

return exports.create(406, message, data);
return internals.create(406, message, data, exports.notAcceptable);
};


exports.proxyAuthRequired = function (message, data) {

return exports.create(407, message, data);
return internals.create(407, message, data, exports.proxyAuthRequired);
};


exports.clientTimeout = function (message, data) {

return exports.create(408, message, data);
return internals.create(408, message, data, exports.clientTimeout);
};


exports.conflict = function (message, data) {

return exports.create(409, message, data);
return internals.create(409, message, data, exports.conflict);
};


exports.resourceGone = function (message, data) {

return exports.create(410, message, data);
return internals.create(410, message, data, exports.resourceGone);
};


exports.lengthRequired = function (message, data) {

return exports.create(411, message, data);
return internals.create(411, message, data, exports.lengthRequired);
};


exports.preconditionFailed = function (message, data) {

return exports.create(412, message, data);
return internals.create(412, message, data, exports.preconditionFailed);
};


exports.entityTooLarge = function (message, data) {

return exports.create(413, message, data);
return internals.create(413, message, data, exports.entityTooLarge);
};


exports.uriTooLong = function (message, data) {

return exports.create(414, message, data);
return internals.create(414, message, data, exports.uriTooLong);
};


exports.unsupportedMediaType = function (message, data) {

return exports.create(415, message, data);
return internals.create(415, message, data, exports.unsupportedMediaType);
};


exports.rangeNotSatisfiable = function (message, data) {

return exports.create(416, message, data);
return internals.create(416, message, data, exports.rangeNotSatisfiable);
};


exports.expectationFailed = function (message, data) {

return exports.create(417, message, data);
return internals.create(417, message, data, exports.expectationFailed);
};

exports.badData = function (message, data) {

return exports.create(422, message, data);
return internals.create(422, message, data, exports.badData);
};


exports.tooManyRequests = function (message, data) {

return exports.create(429, message, data);
return internals.create(429, message, data, exports.tooManyRequests);
};


// 5xx Server Errors

exports.internal = function (message, data, statusCode) {

var error = (data instanceof Error ? exports.wrap(data, statusCode, message) : exports.create(statusCode || 500, message));
return internals.serverError(message, data, statusCode, exports.internal);
};

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

if (data instanceof Error === false) {
var error;
if (data instanceof Error) {
error = exports.wrap(data, statusCode, message);
} else {
error = internals.create(statusCode || 500, message, ctor);
error.data = data;
}

Expand All @@ -271,31 +282,31 @@ exports.internal = function (message, data, statusCode) {

exports.notImplemented = function (message, data) {

return exports.internal(message, data, 501);
return internals.serverError(message, data, 501, exports.notImplemented);
};


exports.badGateway = function (message, data) {

return exports.internal(message, data, 502);
return internals.serverError(message, data, 502, exports.badGateway);
};


exports.serverTimeout = function (message, data) {

return exports.internal(message, data, 503);
return internals.serverError(message, data, 503, exports.serverTimeout);
};


exports.gatewayTimeout = function (message, data) {

return exports.internal(message, data, 504);
return internals.serverError(message, data, 504, exports.gatewayTimeout);
};


exports.badImplementation = function (message, data) {

var err = exports.internal(message, data, 500);
var err = internals.serverError(message, data, 500, exports.badImplementation);
err.isDeveloperError = true;
return err;
};

0 comments on commit 62f51e6

Please sign in to comment.