diff --git a/lib/index.js b/lib/index.js index fb6564a..239a441 100755 --- a/lib/index.js +++ b/lib/index.js @@ -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'); @@ -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); @@ -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; @@ -156,102 +160,102 @@ 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); }; @@ -259,9 +263,16 @@ exports.tooManyRequests = function (message, data) { 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; } @@ -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; };