This repository has been archived by the owner on Dec 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
173 lines (164 loc) · 4.96 KB
/
index.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*! httperr 0.5.0 Original author Alan Plum <[email protected]>. Released into the Public Domain under the UNLICENSE. @preserve */
exports.createHttpError = createHttpError;
exports.HttpError = function HttpError(config) {
if (!config) {
config = {};
} else if (typeof config === 'string') {
config = {message: config};
} else if (config instanceof Error) {
config = {cause: config};
}
this.message = config.message;
this.cause = config.cause;
this.details = config.details;
};
exports.HttpError.prototype = Object.create(Error.prototype);
exports.HttpError.prototype.constructor = exports.HttpError;
function createHttpError(status, title, init) {
function HttpError(config) {
var self = this;
if (!self || Object.getPrototypeOf(self) !== HttpError.prototype) {
self = new HttpError(config);
} else {
exports.HttpError.call(self, config);
if (typeof init === 'function') {
init.call(self, config);
}
}
var err = new Error();
err.name = self.name;
err.message = self.message;
self.stack = err.stack || '';
if (self.stack) {
var stack = self.stack.split('\n');
if (self.cause) {
if (self.cause.stack) {
stack = stack.concat(
('from ' + self.cause.stack).split('\n').map(indent)
);
} else {
stack.push(indent('cause: ' + self.cause));
}
}
self.stack = stack.join('\n');
}
return self;
}
var simpleTitle = simplify(title);
HttpError.prototype = Object.create(exports.HttpError.prototype);
HttpError.prototype.constructor = HttpError;
HttpError.prototype.name = camelCase(simpleTitle);
HttpError.prototype.code = ucUnderscore(simpleTitle);
HttpError.prototype.title = title;
HttpError.prototype.statusCode = status;
return HttpError;
}
function indent(str) {
return ' ' + str;
}
function simplify(str) {
return str.replace(/^An?\s+|'/gi, '').replace(/-/g, ' ');
}
function ucUnderscore(str) {
return str.replace(/\s+/g, '_').toUpperCase();
}
function camelCase(str) {
return str.split(' ').map(titleCase).join('');
}
function lcFirst(str) {
return str.slice(0, 1).toLowerCase() + str.slice(1);
}
function titleCase(str) {
return str.slice(0, 1).toUpperCase() + str.slice(1).toLowerCase();
}
function spread(fn) {
return function(args) {
return fn.apply(this, args);
};
}
[
[400, 'Bad Request'],
[401, 'Unauthorized', function(config) {
this.authenticate = config.authenticate;
}],
[402, 'Payment Required'],
[403, 'Forbidden'],
[404, 'Not Found'],
[405, 'Method Not Allowed', function(config) {
this.allowed = config.allowed;
}],
[406, 'Not Acceptable'],
[407, 'Proxy Authentication Required', function(config) {
this.proxyAuthenticate = config.proxyAuthenticate;
}],
[408, 'Request Timeout'],
[409, 'Conflict'],
[410, 'Gone'],
[411, 'Length Required'],
[412, 'Precondition Failed'],
[413, 'Request Entity Too Large'],
[414, 'Request URI Too Long'],
[415, 'Unsupported Media Type'],
[416, 'Requested Range Not Satisfiable', function(config) {
this.contentRange = config.contentRange;
}],
[417, 'Expectation Failed'],
[418, 'I\'m a Teapot'],
[419, 'Authentication Timeout'],
[420, 'Enhance Your Calm', function(config) {
this.retryAfter = config.retryAfter;
}],
[422, 'Unprocessable Entity'],
[423, 'Locked'],
[424, 'Method Failure'],
[424, 'Failed Dependency'],
[425, 'Unordered Collection'],
[426, 'Upgrade Required'],
[428, 'Precondition Required'],
[429, 'Too Many Requests', function(config) {
this.retryAfter = config.retryAfter;
}],
[431, 'Request Header Fields Too Large'],
[440, 'Login Timeout'],
[444, 'No Response'],
[449, 'Retry With', function(config) {
this.parameters = config.parameters;
}],
[450, 'Blocked By Windows Parental Controls'],
[451, 'Redirect', function(config) {
this.location = config.location;
}],
[451, 'Unavailable For Legal Reasons'],
[494, 'Request Header Too Large'],
[495, 'Cert Error'],
[496, 'No Cert'],
[497, 'HTTP To HTTPS'],
[499, 'Client Closed Request'],
[500, 'Internal Server Error'],
[501, 'Not Implemented'],
[502, 'Bad Gateway'],
[503, 'Service Unavailable', function(config) {
this.retryAfter = config.retryAfter;
}],
[504, 'Gateway Timeout'],
[505, 'HTTP Version Not Supported'],
[506, 'Variant Also Negotiates'],
[507, 'Insufficient Storage'],
[508, 'Loop Detected'],
[509, 'Bandwidth Limit Exceeded'],
[510, 'Not Extended'],
[511, 'Network Authentication Required'],
[520, 'Origin Error'],
[522, 'Connection Timed Out'],
[523, 'Proxy Declined Request'],
[524, 'A Timeout Occured'],
[598, 'Network Read Timeout Error'],
[599, 'Network Connect Timeout Error']
].forEach(spread(function(status, title, fn) {
var HttpError = createHttpError(status, title, fn);
var name = HttpError.prototype.name;
var lcName = lcFirst(name);
exports[lcName] = HttpError;
exports[name] = HttpError;
exports[status] = HttpError;
}));