diff --git a/lib/errors/http_error.js b/lib/errors/http_error.js deleted file mode 100644 index d4e97efe0..000000000 --- a/lib/errors/http_error.js +++ /dev/null @@ -1,153 +0,0 @@ -// Copyright 2012 Mark Cavage, Inc. All rights reserved. - -// Because we are constructing error objects dynamically, we use an anonymous -// function as the 'base constructor' then use arguments.callee to fill that in. -// strict mode disallows agruments.callee, disable both of these rules. - -/* eslint-disable strict, no-caller */ - -var http = require('http'); -var util = require('util'); - -var assert = require('assert-plus'); -var WError = require('verror').WError; - - -///--- Globals - -var slice = Function.prototype.call.bind(Array.prototype.slice); - - -///--- Helpers - -/** - * used to programatically create http error code names, using the underlying - * status codes names exposed via the http module. - * @private - * @function codeToErrorName - * @param {Number} code the http error code to dynamically create - * @returns {String} - */ -function codeToErrorName(code) { - code = parseInt(code, 10); - var status = http.STATUS_CODES[code]; - - if (!status) { - return (false); - } - - - var pieces = status.split(/\s+/); - var str = ''; - pieces.forEach(function (s) { - str += s.charAt(0).toUpperCase() + s.slice(1).toLowerCase(); - }); - - str = str.replace(/\W+/g, ''); - - if (!/\w+Error$/.test(str)) { - str += 'Error'; - } - - return (str); -} - - -///--- Error Base class - -/** - * HttpError class. inherits from WError. - * @public - * @class - * @param {Object} options an options object - */ -function HttpError(options) { - assert.object(options, 'options'); - - options.constructorOpt = options.constructorOpt || HttpError; - WError.apply(this, arguments); - - var self = this; - var code = parseInt((options.statusCode || 500), 10); - this.statusCode = code; - this.body = options.body || { - code: codeToErrorName(code), - message: options.message || self.message - }; - this.message = options.message || self.message; -} -util.inherits(HttpError, WError); - - -///--- Exports - -module.exports = { - - HttpError: HttpError, - - codeToHttpError: function codeToHttpError(code, message, body) { - var err; - var name = codeToErrorName(code); - - if (!name) { - err = new HttpError({ - statusCode: code, - message: message, - body: body - }); - err.name = 'Http' + code + 'Error'; - } else { - err = new module.exports[name]({ - body: body, - message: message, - constructorOpt: codeToHttpError, - statusCode: code - }); - } - - return (err); - } - -}; - - -// Export all the 4xx and 5xx HTTP Status codes as Errors -var codes = Object.keys(http.STATUS_CODES); - -codes.forEach(function (code) { - if (code < 400) { - return; - } - - var name = codeToErrorName(code); - - module.exports[name] = function (cause, message) { - var index = 1; - var opts = { - statusCode: code - }; - - if (cause && cause instanceof Error) { - opts.cause = cause; - opts.constructorOpt = arguments.callee; - } else if (typeof (cause) === 'object') { - opts.body = cause.body; - opts.cause = cause.cause; - opts.constructorOpt = cause.constructorOpt; - opts.message = cause.message; - opts.statusCode = cause.statusCode || code; - } else { - opts.constructorOpt = arguments.callee; - index = 0; - } - - var args = slice(arguments, index); - args.unshift(opts); - HttpError.apply(this, args); - }; - util.inherits(module.exports[name], HttpError); - - module.exports[name].displayName = - module.exports[name].prototype.name = - name; -}); diff --git a/lib/errors/index.js b/lib/errors/index.js deleted file mode 100644 index d014af482..000000000 --- a/lib/errors/index.js +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2012 Mark Cavage, Inc. All rights reserved. - -'use strict'; - -var httpErrors = require('./http_error'); -var restErrors = require('./rest_error'); - - -module.exports = {}; - -Object.keys(httpErrors).forEach(function (k) { - module.exports[k] = httpErrors[k]; -}); - -// Note some of the RestErrors overwrite plain HTTP errors. -Object.keys(restErrors).forEach(function (k) { - module.exports[k] = restErrors[k]; -}); diff --git a/lib/errors/rest_error.js b/lib/errors/rest_error.js deleted file mode 100644 index 3f8378a9c..000000000 --- a/lib/errors/rest_error.js +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright 2012 Mark Cavage, Inc. All rights reserved. - -// Because we are constructing error objects dynamically, we use an anonymous -// function as the 'base constructor' then use arguments.callee to fill that in. -// strict mode disallows agruments.callee, disable both of these rules. - -/* eslint-disable strict, no-caller */ - -var util = require('util'); - -var assert = require('assert-plus'); - -var httpErrors = require('./http_error'); - - -///--- Globals - -var slice = Function.prototype.call.bind(Array.prototype.slice); - -var HttpError = httpErrors.HttpError; - -var CODES = { - BadDigest: 400, - BadMethod: 405, - Internal: 500, // Don't have InternalErrorError - InvalidArgument: 409, - InvalidContent: 400, - InvalidCredentials: 401, - InvalidHeader: 400, - InvalidVersion: 400, - MissingParameter: 409, - NotAuthorized: 403, - PreconditionFailed: 412, - RequestExpired: 400, - RequestThrottled: 429, - ResourceNotFound: 404, - WrongAccept: 406 -}; - - -///--- API - -function RestError(options) { - assert.object(options, 'options'); - - options.constructorOpt = options.constructorOpt || RestError; - HttpError.apply(this, arguments); - - var self = this; - this.restCode = options.restCode || 'Error'; - this.body = options.body || { - code: self.restCode, - message: options.message || self.message - }; -} -util.inherits(RestError, HttpError); - - -///--- Exports - -module.exports = { - RestError: RestError -}; - -Object.keys(CODES).forEach(function (k) { - var name = k; - - if (!/\w+Error$/.test(name)) { - name += 'Error'; - } - - module.exports[name] = function (cause, message) { - var index = 1; - var opts = { - restCode: (k === 'Internal' ? 'InternalError' : k), - statusCode: CODES[k] - }; - - opts.constructorOpt = arguments.callee; - - if (cause && cause instanceof Error) { - opts.cause = cause; - } else if (typeof (cause) === 'object') { - opts.body = cause.body; - opts.cause = cause.cause; - opts.message = cause.message; - opts.statusCode = cause.statusCode || CODES[k]; - } else { - index = 0; - } - - var args = slice(arguments, index); - args.unshift(opts); - RestError.apply(this, args); - }; - util.inherits(module.exports[name], RestError); - module.exports[name].displayName = - module.exports[name].prototype.name = - name; -}); diff --git a/lib/index.js b/lib/index.js index 4219a61de..670524c08 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,14 +2,14 @@ 'use strict'; +var errors = require('restify-errors'); + var bunyan = require('./bunyan_helper'); -var InternalError = require('./errors').InternalError; var Router = require('./router'); var Server = require('./server'); - var shallowCopy = require('./utils').shallowCopy; - +var InternalError = errors.InternalError; /** * creates a server. @@ -19,6 +19,7 @@ var shallowCopy = require('./utils').shallowCopy; * @returns {Server} */ function createServer(options) { + var opts = shallowCopy(options || {}); var server; @@ -74,12 +75,6 @@ module.exports = { }; -var errors = require('./errors'); -Object.keys(errors).forEach(function (k) { - module.exports.errors[k] = errors[k]; - module.exports[k] = errors[k]; -}); - if (!process.env.RESTIFY_CLIENT_ONLY) { module.exports.createServer = createServer; diff --git a/lib/plugins/accept.js b/lib/plugins/accept.js index 82dad9431..c5eff1312 100644 --- a/lib/plugins/accept.js +++ b/lib/plugins/accept.js @@ -5,7 +5,7 @@ var assert = require('assert-plus'); var mime = require('mime'); -var NotAcceptableError = require('../errors').NotAcceptableError; +var NotAcceptableError = require('restify-errors').NotAcceptableError; /** diff --git a/lib/plugins/audit.js b/lib/plugins/audit.js index 5af8d5a9b..d5afcaa3d 100644 --- a/lib/plugins/audit.js +++ b/lib/plugins/audit.js @@ -4,8 +4,7 @@ var assert = require('assert-plus'); var bunyan = require('bunyan'); - -var HttpError = require('../errors').HttpError; +var HttpError = require('restify-errors').HttpError; ///--- API diff --git a/lib/plugins/authorization.js b/lib/plugins/authorization.js index 172cc974f..eeb52c21e 100644 --- a/lib/plugins/authorization.js +++ b/lib/plugins/authorization.js @@ -3,8 +3,7 @@ 'use strict'; var httpSignature = require('http-signature'); - -var errors = require('../errors'); +var errors = require('restify-errors'); ///--- Globals diff --git a/lib/plugins/body_parser.js b/lib/plugins/body_parser.js index 5179df8ea..0d6589bc7 100644 --- a/lib/plugins/body_parser.js +++ b/lib/plugins/body_parser.js @@ -3,8 +3,7 @@ 'use strict'; var assert = require('assert-plus'); - -var errors = require('../errors'); +var errors = require('restify-errors'); var bodyReader = require('./body_reader'); var jsonParser = require('./json_body_parser'); diff --git a/lib/plugins/body_reader.js b/lib/plugins/body_reader.js index 516cdaf69..296733db0 100644 --- a/lib/plugins/body_reader.js +++ b/lib/plugins/body_reader.js @@ -6,8 +6,7 @@ var crypto = require('crypto'); var zlib = require('zlib'); var assert = require('assert-plus'); - -var errors = require('../errors'); +var errors = require('restify-errors'); ///--- Globals diff --git a/lib/plugins/conditional_request.js b/lib/plugins/conditional_request.js index 2a5b0d4cf..f5192a0e5 100644 --- a/lib/plugins/conditional_request.js +++ b/lib/plugins/conditional_request.js @@ -2,7 +2,7 @@ 'use strict'; -var errors = require('../errors'); +var errors = require('restify-errors'); ///--- Globals diff --git a/lib/plugins/date.js b/lib/plugins/date.js index 99d808c00..ee59faec9 100644 --- a/lib/plugins/date.js +++ b/lib/plugins/date.js @@ -3,8 +3,7 @@ 'use strict'; var assert = require('assert-plus'); - -var errors = require('../errors'); +var errors = require('restify-errors'); ///--- Globals diff --git a/lib/plugins/form_body_parser.js b/lib/plugins/form_body_parser.js index 2c8165719..3f78230d6 100644 --- a/lib/plugins/form_body_parser.js +++ b/lib/plugins/form_body_parser.js @@ -6,7 +6,7 @@ var assert = require('assert-plus'); var querystring = require('qs'); var bodyReader = require('./body_reader'); -var errors = require('../errors'); +var errors = require('restify-errors'); ///--- Globals diff --git a/lib/plugins/json_body_parser.js b/lib/plugins/json_body_parser.js index d0fcc68f8..e885bf316 100644 --- a/lib/plugins/json_body_parser.js +++ b/lib/plugins/json_body_parser.js @@ -3,9 +3,9 @@ 'use strict'; var assert = require('assert-plus'); +var errors = require('restify-errors'); var bodyReader = require('./body_reader'); -var errors = require('../errors'); ///--- API diff --git a/lib/plugins/multipart_body_parser.js b/lib/plugins/multipart_body_parser.js index 108ccd553..6ba041cea 100644 --- a/lib/plugins/multipart_body_parser.js +++ b/lib/plugins/multipart_body_parser.js @@ -7,9 +7,9 @@ var fs = require('fs'); var assert = require('assert-plus'); var formidable = require('formidable'); var once = require('once'); +var errors = require('restify-errors'); var vasync = require('vasync'); -var errors = require('../errors'); diff --git a/lib/plugins/request_expiry.js b/lib/plugins/request_expiry.js index 8bd7d1b97..cb5c82110 100644 --- a/lib/plugins/request_expiry.js +++ b/lib/plugins/request_expiry.js @@ -1,7 +1,7 @@ 'use strict'; var assert = require('assert-plus'); -var GatewayTimeoutError = require('./../errors').GatewayTimeoutError; +var GatewayTimeoutError = require('restify-errors').GatewayTimeoutError; /** * A request expiry will use the headers to tell if the diff --git a/lib/plugins/static.js b/lib/plugins/static.js index f51ccc1a3..15c4921bf 100644 --- a/lib/plugins/static.js +++ b/lib/plugins/static.js @@ -8,7 +8,7 @@ var escapeRE = require('escape-regexp-component'); var assert = require('assert-plus'); var mime = require('mime'); -var errors = require('../errors'); +var errors = require('restify-errors'); ///--- Globals diff --git a/lib/plugins/throttle.js b/lib/plugins/throttle.js index 0d61aabfc..a1c96729d 100644 --- a/lib/plugins/throttle.js +++ b/lib/plugins/throttle.js @@ -6,8 +6,7 @@ var sprintf = require('util').format; var assert = require('assert-plus'); var LRU = require('lru-cache'); - -var errors = require('../errors'); +var errors = require('restify-errors'); ///--- Globals diff --git a/lib/response.js b/lib/response.js index 810038410..fd33eb282 100644 --- a/lib/response.js +++ b/lib/response.js @@ -8,10 +8,10 @@ var url = require('url'); var assert = require('assert-plus'); var mime = require('mime'); +var errors = require('restify-errors'); var httpDate = require('./http_date'); var utils = require('./utils'); -var errors = require('./errors'); ///--- Globals diff --git a/lib/router.js b/lib/router.js index edd6b76f9..49d2c3dd7 100644 --- a/lib/router.js +++ b/lib/router.js @@ -6,13 +6,13 @@ var EventEmitter = require('events').EventEmitter; var url = require('url'); var util = require('util'); +var errors = require('restify-errors'); var assert = require('assert-plus'); var LRU = require('lru-cache'); var Negotiator = require('negotiator'); var semver = require('semver'); var cors = require('./plugins/cors'); -var errors = require('./errors'); var utils = require('./utils'); diff --git a/lib/server.js b/lib/server.js index de0b5fe12..de80199bd 100644 --- a/lib/server.js +++ b/lib/server.js @@ -8,6 +8,7 @@ var http = require('http'); var https = require('https'); var util = require('util'); +var errors = require('restify-errors'); var assert = require('assert-plus'); var mime = require('mime'); var once = require('once'); @@ -15,7 +16,6 @@ var spdy = require('spdy'); var uuid = require('node-uuid'); var dtrace = require('./dtrace'); -var errors = require('./errors'); var formatters = require('./formatters'); var shallowCopy = require('./utils').shallowCopy; var upgrade = require('./upgrade'); diff --git a/package.json b/package.json index ae6febba9..15401788d 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "node-uuid": "^1.4.1", "once": "^1.3.0", "restify-clients": "1.0.0", + "restify-errors": "3.0.0", "qs": "^3.1.0", "semver": "^4.3.3", "spdy": "^1.26.5", diff --git a/test/response.test.js b/test/response.test.js index ca8eb3f19..6ceeca3d1 100644 --- a/test/response.test.js +++ b/test/response.test.js @@ -279,7 +279,7 @@ test('redirect using options.url', function (t) { // jscs:disable maximumLineLength -test('redirect should cause InternalServerError when invoked without next', function (t) { +test('redirect should cause InternalError when invoked without next', function (t) { SERVER.get('/9', function (req, res, next) { res.redirect(); @@ -290,7 +290,7 @@ test('redirect should cause InternalServerError when invoked without next', func // json parse the response var msg = JSON.parse(res.body); - t.equal(msg.code, 'InternalError'); + t.equal(msg.code, 'Internal'); t.end(); }); }); diff --git a/test/server.test.js b/test/server.test.js index ed5858f5c..3360e2aef 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -8,8 +8,9 @@ var http = require('http'); var filed = require('filed'); var restifyClients = require('restify-clients'); var uuid = require('node-uuid'); +var errors = require('restify-errors'); -var RestError = require('../lib/errors').RestError; +var RestError = errors.RestError; var restify = require('../lib'); if (require.cache[__dirname + '/lib/helper.js']) { @@ -1082,8 +1083,9 @@ test('next.ifError', function (t) { process.nextTick(function () { var e = new RestError({ statusCode: 400, - restCode: 'Foo' - }, 'screw you client'); + restCode: 'Foo', + message: 'screw you client' + }); next.ifError(e); t.notOk(true); res.send(200); @@ -1779,7 +1781,7 @@ test('fire event on error', function (t) { }); SERVER.get('/', function (req, res, next) { - return (next(new restify.errors.InternalServerError('bah!'))); + return (next(new errors.InternalServerError('bah!'))); }); CLIENT.get('/', function (err, _, res) {