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