« index
Coverage for /Users/yunong/workspace/node-restify/lib/errors/http_error.js : 97%
128 lines |
125 run |
3 missing |
1 partial |
14 blocks |
13 blocks run |
1 blocks missing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | // Copyright 2012 Mark Cavage, Inc. All rights reserved. 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 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 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; }); |