Initial commit
[yaffs-website] / node_modules / http-errors / index.js
1
2 var statuses = require('statuses');
3 var inherits = require('inherits');
4
5 function toIdentifier(str) {
6   return str.split(' ').map(function (token) {
7     return token.slice(0, 1).toUpperCase() + token.slice(1)
8   }).join('').replace(/[^ _0-9a-z]/gi, '')
9 }
10
11 exports = module.exports = function httpError() {
12   // so much arity going on ~_~
13   var err;
14   var msg;
15   var status = 500;
16   var props = {};
17   for (var i = 0; i < arguments.length; i++) {
18     var arg = arguments[i];
19     if (arg instanceof Error) {
20       err = arg;
21       status = err.status || err.statusCode || status;
22       continue;
23     }
24     switch (typeof arg) {
25       case 'string':
26         msg = arg;
27         break;
28       case 'number':
29         status = arg;
30         break;
31       case 'object':
32         props = arg;
33         break;
34     }
35   }
36
37   if (typeof status !== 'number' || !statuses[status]) {
38     status = 500
39   }
40
41   // constructor
42   var HttpError = exports[status]
43
44   if (!err) {
45     // create error
46     err = HttpError
47       ? new HttpError(msg)
48       : new Error(msg || statuses[status])
49     Error.captureStackTrace(err, httpError)
50   }
51
52   if (!HttpError || !(err instanceof HttpError)) {
53     // add properties to generic error
54     err.expose = status < 500
55     err.status = err.statusCode = status
56   }
57
58   for (var key in props) {
59     if (key !== 'status' && key !== 'statusCode') {
60       err[key] = props[key]
61     }
62   }
63
64   return err;
65 };
66
67 // create generic error objects
68 var codes = statuses.codes.filter(function (num) {
69   return num >= 400;
70 });
71
72 codes.forEach(function (code) {
73   var name = toIdentifier(statuses[code])
74   var className = name.match(/Error$/) ? name : name + 'Error'
75
76   if (code >= 500) {
77     var ServerError = function ServerError(msg) {
78       var self = new Error(msg != null ? msg : statuses[code])
79       Error.captureStackTrace(self, ServerError)
80       self.__proto__ = ServerError.prototype
81       Object.defineProperty(self, 'name', {
82         enumerable: false,
83         configurable: true,
84         value: className,
85         writable: true
86       })
87       return self
88     }
89     inherits(ServerError, Error);
90     ServerError.prototype.status =
91     ServerError.prototype.statusCode = code;
92     ServerError.prototype.expose = false;
93     exports[code] =
94     exports[name] = ServerError
95     return;
96   }
97
98   var ClientError = function ClientError(msg) {
99     var self = new Error(msg != null ? msg : statuses[code])
100     Error.captureStackTrace(self, ClientError)
101     self.__proto__ = ClientError.prototype
102     Object.defineProperty(self, 'name', {
103       enumerable: false,
104       configurable: true,
105       value: className,
106       writable: true
107     })
108     return self
109   }
110   inherits(ClientError, Error);
111   ClientError.prototype.status =
112   ClientError.prototype.statusCode = code;
113   ClientError.prototype.expose = true;
114   exports[code] =
115   exports[name] = ClientError
116   return;
117 });
118
119 // backwards-compatibility
120 exports["I'mateapot"] = exports.ImATeapot