PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/error.js

https://github.com/goffrie/node-github
JavaScript | 117 lines | 83 code | 13 blank | 21 comment | 3 complexity | 06acfe712ffd3d84ec44316295532a00 MD5 | raw file
  1/** section: github
  2 * class HttpError
  3 * 
  4 *  Copyright 2012 Cloud9 IDE, Inc.
  5 *
  6 *  This product includes software developed by
  7 *  Cloud9 IDE, Inc (http://c9.io).
  8 *
  9 *  Author: Mike de Boer <mike@c9.io>
 10 **/
 11
 12var Util = require("util");
 13
 14exports.HttpError = function(message, code) {
 15    Error.call(this, message);
 16    //Error.captureStackTrace(this, arguments.callee);
 17    this.message = message;
 18    this.code = code;
 19};
 20Util.inherits(exports.HttpError, Error);
 21
 22(function() {
 23    /**
 24     *  HttpError#toString() -> String
 25     * 
 26     *  Returns the stringified version of the error (i.e. the message).
 27     **/
 28    this.toString = function() {
 29        return this.message;
 30    };
 31    
 32    /**
 33     *  HttpError#toJSON() -> Object
 34     * 
 35     *  Returns a JSON object representation of the error.
 36     **/
 37    this.toJSON = function() {
 38        return {
 39            code: this.code,
 40            status: this.defaultMessage,
 41            message: this.message
 42        };
 43    };
 44    
 45}).call(exports.HttpError.prototype);
 46
 47
 48var statusCodes = {
 49    400: "Bad Request",
 50    401: "Unauthorized",
 51    402: "Payment Required",
 52    403: "Forbidden",
 53    404: "Not Found",
 54    405: "Method Not Allowed",
 55    406: "Not Acceptable",
 56    407: "Proxy Authentication Required",
 57    408: "Request Timeout",
 58    409: "Conflict",
 59    410: "Gone",
 60    411: "Length Required",
 61    412: "Precondition Failed",
 62    413: "Request Entity Too Large",
 63    414: "Request-URI Too Long",
 64    415: "Unsupported Media Type",
 65    416: "Requested Range Not Satisfiable",
 66    417: "Expectation Failed",
 67    420: "Enhance Your Calm",
 68    422: "Unprocessable Entity",
 69    423: "Locked",
 70    424: "Failed Dependency",
 71    425: "Unordered Collection",
 72    426: "Upgrade Required",
 73    428: "Precondition Required",
 74    429: "Too Many Requests",
 75    431: "Request Header Fields Too Large",
 76    444: "No Response",
 77    449: "Retry With",
 78    499: "Client Closed Request",
 79    500: "Internal Server Error",
 80    501: "Not Implemented",
 81    502: "Bad Gateway",
 82    503: "Service Unavailable",
 83    504: "Gateway Timeout",
 84    505: "HTTP Version Not Supported",
 85    506: "Variant Also Negotiates",
 86    507: "Insufficient Storage",
 87    508: "Loop Detected",
 88    509: "Bandwidth Limit Exceeded",
 89    510: "Not Extended",
 90    511: "Network Authentication Required"
 91};
 92
 93for (var status in statusCodes) {
 94    var defaultMsg = statusCodes[status];
 95    
 96    var error = (function(defaultMsg, status) {
 97        return function(msg) {
 98            this.defaultMessage = defaultMsg;
 99            exports.HttpError.call(this, msg || status + ": " + defaultMsg, status);
100            
101            if (status >= 500)
102                Error.captureStackTrace(this, arguments.callee);
103        };
104    })(defaultMsg, status);
105    
106    Util.inherits(error, exports.HttpError);
107    
108    var className = toCamelCase(defaultMsg);
109    exports[className] = error;
110    exports[status] = error;
111}
112
113function toCamelCase(str) {
114    return str.toLowerCase().replace(/(?:(^.)|(\s+.))/g, function(match) {
115        return match.charAt(match.length-1).toUpperCase();
116    });
117}