Name: js-handler/node_modules/restify/lib/errors/http_error.js
| 1: | // Copyright 2012 Mark Cavage, Inc. All rights reserved. |
| 2: | |
| 3: | var http = require('http'); |
| 4: | var util = require('util'); |
| 5: | |
| 6: | var assert = require('assert-plus'); |
| 7: | var WError = require('verror').WError; |
| 8: | |
| 9: | |
| 10: | |
| 11: | ///--- Globals |
| 12: | |
| 13: | var slice = Function.prototype.call.bind(Array.prototype.slice); |
| 14: | |
| 15: | |
| 16: | |
| 17: | ///--- Helpers |
| 18: | |
| 19: | function codeToErrorName(code) { |
| 20: | code = parseInt(code, 10); |
| 21: | var status = http.STATUS_CODES[code]; |
| 22: | if (!status) |
| 23: | return (false); |
| 24: | |
| 25: | |
| 26: | var pieces = status.split(/\s+/); |
| 27: | var str = ''; |
| 28: | pieces.forEach(function (s) { |
| 29: | str += s.charAt(0).toUpperCase() + s.slice(1).toLowerCase(); |
| 30: | }); |
| 31: | |
| 32: | str = str.replace(/\W+/g, ''); |
| 33: | if (!/\w+Error$/.test(str)) |
| 34: | str += 'Error'; |
| 35: | |
| 36: | return (str); |
| 37: | } |
| 38: | |
| 39: | |
| 40: | |
| 41: | ///--- Error Base class |
| 42: | |
| 43: | function HttpError(options) { |
| 44: | assert.object(options, 'options'); |
| 45: | |
| 46: | options.constructorOpt = options.constructorOpt || HttpError; |
| 47: | WError.apply(this, arguments); |
| 48: | |
| 49: | var self = this; |
| 50: | var code = parseInt((options.statusCode || 500), 10); |
| 51: | this.statusCode = code; |
| 52: | this.body = options.body || { |
| 53: | code: codeToErrorName(code), |
| 54: | message: options.message || self.message |
| 55: | }; |
| 56: | this.message = options.message || self.message; |
| 57: | } |
| 58: | util.inherits(HttpError, WError); |
| 59: | |
| 60: | |
| 61: | |
| 62: | ///--- Exports |
| 63: | |
| 64: | module.exports = { |
| 65: | |
| 66: | HttpError: HttpError, |
| 67: | |
| 68: | codeToHttpError: function codeToHttpError(code, message, body) { |
| 69: | var err; |
| 70: | var name = codeToErrorName(code); |
| 71: | |
| 72: | if (!name) { |
| 73: | err = new HttpError({ |
| 74: | statusCode: code, |
| 75: | message: message, |
| 76: | body: body |
| 77: | }); |
| 78: | err.name = 'Http' + code + 'Error'; |
| 79: | } else { |
| 80: | err = new module.exports[name]({ |
| 81: | body: body, |
| 82: | message: message, |
| 83: | constructorOpt: codeToHttpError, |
| 84: | statusCode: code |
| 85: | }); |
| 86: | } |
| 87: | |
| 88: | return (err); |
| 89: | } |
| 90: | |
| 91: | }; |
| 92: | |
| 93: | |
| 94: | |
| 95: | // Export all the 4xx and 5xx HTTP Status codes as Errors |
| 96: | var codes = Object.keys(http.STATUS_CODES); |
| 97: | |
| 98: | codes.forEach(function (code) { |
| 99: | if (code < 400) |
| 100: | return; |
| 101: | |
| 102: | var name = codeToErrorName(code); |
| 103: | |
| 104: | module.exports[name] = function (cause, message) { |
| 105: | var index = 1; |
| 106: | var opts = { |
| 107: | statusCode: code |
| 108: | }; |
| 109: | |
| 110: | if (cause && cause instanceof Error) { |
| 111: | opts.cause = cause; |
| 112: | opts.constructorOpt = arguments.callee; |
| 113: | } else if (typeof (cause) === 'object') { |
| 114: | opts.body = cause.body; |
| 115: | opts.cause = cause.cause; |
| 116: | opts.constructorOpt = cause.constructorOpt; |
| 117: | opts.message = cause.message; |
| 118: | opts.statusCode = cause.statusCode || code; |
| 119: | } else { |
| 120: | opts.constructorOpt = arguments.callee; |
| 121: | index = 0; |
| 122: | } |
| 123: | |
| 124: | var args = slice(arguments, index); |
| 125: | args.unshift(opts); |
| 126: | HttpError.apply(this, args); |
| 127: | }; |
| 128: | util.inherits(module.exports[name], HttpError); |
| 129: | |
| 130: | module.exports[name].displayName = |
| 131: | module.exports[name].prototype.name = |
| 132: | name; |
| 133: | }); |
