-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
338 lines (293 loc) · 9.74 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const decycle = require('./decycle');
const isStrict = (function () { return !this; })();
function AuthTokenExpiredError(message, expiry) {
this.name = 'AuthTokenExpiredError';
this.message = message;
this.expiry = expiry;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
AuthTokenExpiredError.prototype = Object.create(Error.prototype);
function AuthTokenInvalidError(message) {
this.name = 'AuthTokenInvalidError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
AuthTokenInvalidError.prototype = Object.create(Error.prototype);
function AuthTokenNotBeforeError(message, date) {
this.name = 'AuthTokenNotBeforeError';
this.message = message;
this.date = date;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
AuthTokenNotBeforeError.prototype = Object.create(Error.prototype);
// For any other auth token error.
function AuthTokenError(message) {
this.name = 'AuthTokenError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
AuthTokenError.prototype = Object.create(Error.prototype);
// For any other auth error; not specifically related to the auth token itself.
function AuthError(message) {
this.name = 'AuthError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
AuthError.prototype = Object.create(Error.prototype);
function SilentMiddlewareBlockedError(message, type) {
this.name = 'SilentMiddlewareBlockedError';
this.message = message;
this.type = type;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
SilentMiddlewareBlockedError.prototype = Object.create(Error.prototype);
function InvalidActionError(message) {
this.name = 'InvalidActionError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
InvalidActionError.prototype = Object.create(Error.prototype);
function InvalidArgumentsError(message) {
this.name = 'InvalidArgumentsError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
InvalidArgumentsError.prototype = Object.create(Error.prototype);
function InvalidOptionsError(message) {
this.name = 'InvalidOptionsError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
InvalidOptionsError.prototype = Object.create(Error.prototype);
function InvalidMessageError(message) {
this.name = 'InvalidMessageError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
InvalidMessageError.prototype = Object.create(Error.prototype);
function SocketProtocolError(message, code) {
this.name = 'SocketProtocolError';
this.message = message;
this.code = code;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
SocketProtocolError.prototype = Object.create(Error.prototype);
function ServerProtocolError(message) {
this.name = 'ServerProtocolError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
ServerProtocolError.prototype = Object.create(Error.prototype);
function HTTPServerError(message) {
this.name = 'HTTPServerError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
HTTPServerError.prototype = Object.create(Error.prototype);
function ResourceLimitError(message) {
this.name = 'ResourceLimitError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
ResourceLimitError.prototype = Object.create(Error.prototype);
function TimeoutError(message) {
this.name = 'TimeoutError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
TimeoutError.prototype = Object.create(Error.prototype);
function BadConnectionError(message, type, code, reason) {
this.name = 'BadConnectionError';
this.message = message;
this.type = type;
this.code = code || 1001;
this.reason = reason || socketProtocolIgnoreStatuses[this.code] || '';
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
BadConnectionError.prototype = Object.create(Error.prototype);
function BrokerError(message) {
this.name = 'BrokerError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
BrokerError.prototype = Object.create(Error.prototype);
function ProcessExitError(message, code) {
this.name = 'ProcessExitError';
this.message = message;
this.code = code;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
ProcessExitError.prototype = Object.create(Error.prototype);
function UnknownError(message) {
this.name = 'UnknownError';
this.message = message;
if (Error.captureStackTrace && !isStrict) {
Error.captureStackTrace(this, arguments.callee);
} else {
this.stack = (new Error()).stack;
}
}
UnknownError.prototype = Object.create(Error.prototype);
// Expose all error types.
module.exports = {
AuthTokenExpiredError: AuthTokenExpiredError,
AuthTokenInvalidError: AuthTokenInvalidError,
AuthTokenNotBeforeError: AuthTokenNotBeforeError,
AuthTokenError: AuthTokenError,
AuthError: AuthError,
SilentMiddlewareBlockedError: SilentMiddlewareBlockedError,
InvalidActionError: InvalidActionError,
InvalidArgumentsError: InvalidArgumentsError,
InvalidOptionsError: InvalidOptionsError,
InvalidMessageError: InvalidMessageError,
SocketProtocolError: SocketProtocolError,
ServerProtocolError: ServerProtocolError,
HTTPServerError: HTTPServerError,
ResourceLimitError: ResourceLimitError,
TimeoutError: TimeoutError,
BadConnectionError: BadConnectionError,
BrokerError: BrokerError,
ProcessExitError: ProcessExitError,
UnknownError: UnknownError
};
const socketProtocolErrorStatuses = {
1001: 'Socket was disconnected',
1002: 'A WebSocket protocol error was encountered',
1003: 'Server terminated socket because it received invalid data',
1005: 'Socket closed without status code',
1006: 'Socket hung up',
1007: 'Message format was incorrect',
1008: 'Encountered a policy violation',
1009: 'Message was too big to process',
1010: 'Client ended the connection because the server did not comply with extension requirements',
1011: 'Server encountered an unexpected fatal condition',
4000: 'Server ping timed out',
4001: 'Client pong timed out',
4002: 'Server failed to sign auth token',
4003: 'Failed to complete handshake',
4004: 'Client failed to save auth token',
4005: 'Did not receive #handshake from client before timeout',
4006: 'Failed to bind socket to message broker',
4007: 'Client connection establishment timed out',
4008: 'Server rejected handshake from client',
4009: 'Server received a message before the client handshake'
};
const socketProtocolIgnoreStatuses = {
1000: 'Socket closed normally',
1001: socketProtocolErrorStatuses[1001]
};
module.exports.socketProtocolErrorStatuses = socketProtocolErrorStatuses;
module.exports.socketProtocolIgnoreStatuses = socketProtocolIgnoreStatuses;
// Convert an error into a JSON-compatible type which can later be hydrated
// back to its *original* form.
module.exports.dehydrateError = function dehydrateError(error) {
let dehydratedError;
if (error && typeof error === 'object') {
dehydratedError = {
message: error.message
};
for (let i of Object.keys(error)) {
dehydratedError[i] = error[i];
}
} else if (typeof error === 'function') {
dehydratedError = '[function ' + (typeof error.name === 'string' ? error.name : 'anonymous') + ']';
} else {
dehydratedError = error;
}
return decycle(dehydratedError);
};
// Convert a dehydrated error back to its *original* form.
module.exports.hydrateError = function hydrateError(error) {
let hydratedError = null;
if (error != null) {
if (typeof error === 'object') {
hydratedError = new Error(
typeof error.message === 'string' ? error.message : 'Invalid error message format'
);
if (typeof error.name === 'string') {
hydratedError.name = error.name;
}
for (let i of Object.keys(error)) {
if (hydratedError[i] === undefined) {
hydratedError[i] = error[i];
}
}
} else {
hydratedError = error;
}
}
return hydratedError;
};
module.exports.decycle = decycle;