-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy patherrors.js
353 lines (306 loc) · 8.49 KB
/
errors.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
'use strict'
var util = require('./_util')
/**
* FaunaDB error types. Request errors can originate from the client (e.g. bad
* method parameters) or from the FaunaDB Server (e.g. invalid queries,
* timeouts.) Server errors will subclass {@link module:errors~FaunaHTTPError}.
* Stream errors will subclass {@link module:errors~StreamError}.
*
* @module errors
*/
/**
* The base exception type for all FaunaDB errors.
*
* @param {string} message
* @extends Error
* @constructor
*/
function FaunaError(name, message, description) {
Error.call(this)
/**
* Name of this exception.
* @type {string}
*/
this.name = name
/**
* Message for this exception.
* @type {string}
*/
this.message = message
/**
* Description for this exception.
* @type {string}
*/
this.description = description
}
util.inherits(FaunaError, Error)
/**
* Exception thrown by this client library when an invalid
* value is provided as a function argument.
*
* @extends module:errors~FaunaError
* @constructor
*/
function InvalidValue(message) {
FaunaError.call(this, 'InvalidValue', message)
}
util.inherits(InvalidValue, FaunaError)
/**
* Exception thrown by this client library when an invalid
* value is provided as a function argument.
*
* @extends module:errors~FaunaError
* @constructor
*/
function InvalidArity(min, max, actual, callerFunc) {
var arityInfo = `${callerFunc} function requires ${messageForArity(
min,
max
)} argument(s) but ${actual} were given`
var documentationLink = logDocumentationLink(callerFunc)
FaunaError.call(this, 'InvalidArity', `${arityInfo}\n${documentationLink}`)
/**
* Minimum number of arguments.
* @type {number}
*/
this.min = min
/**
* Maximum number of arguments.
* @type {number}
*/
this.max = max
/**
* Actual number of arguments called with.
* @type {number}
*/
this.actual = actual
function messageForArity(min, max) {
if (max === null) return 'at least ' + min
if (min === null) return 'up to ' + max
if (min === max) return min
return 'from ' + min + ' to ' + max
}
function logDocumentationLink(functionName) {
var docsURL = 'https://docs.fauna.com/fauna/current/api/fql/functions/'
return `For more info, see the docs: ${docsURL}${functionName.toLowerCase()}`
}
}
util.inherits(InvalidArity, FaunaError)
/**
* Base exception type for errors returned by the FaunaDB server.
*
* @param {RequestResult} requestResult
*
* @extends module:errors~FaunaError
* @constructor
*/
function FaunaHTTPError(name, requestResult) {
var response = requestResult.responseContent
var errors = response.errors
var message = errors.length === 0 ? '(empty "errors")' : errors[0].code
var description =
errors.length === 0 ? '(empty "errors")' : errors[0].description
FaunaError.call(this, name, message, description)
/**
* A wrapped {@link RequestResult} object, containing the request and response
* context of the failed request.
*
* @type {RequestResult}
*/
this.requestResult = requestResult
}
util.inherits(FaunaHTTPError, FaunaError)
/**
* Convenience method to return the errors from the response object.
*
* @returns {Object}
*/
FaunaHTTPError.prototype.errors = function() {
return this.requestResult.responseContent.errors
}
/**
* Takes a {@link RequestResult} and throws an appropriate exception if
* it contains a failed request.
*
* @param requestResult {RequestResult}
*/
FaunaHTTPError.raiseForStatusCode = function(requestResult) {
var code = requestResult.statusCode
if (code < 200 || code >= 300) {
switch (code) {
case 400:
throw new BadRequest(requestResult)
case 401:
throw new Unauthorized(requestResult)
case 403:
throw new PermissionDenied(requestResult)
case 404:
throw new NotFound(requestResult)
case 405:
throw new MethodNotAllowed(requestResult)
case 429:
throw new TooManyRequests(requestResult)
case 500:
throw new InternalError(requestResult)
case 503:
throw new UnavailableError(requestResult)
default:
throw new FaunaHTTPError('UnknownError', requestResult)
}
}
}
/**
* A HTTP 400 error.
*
* @param {RequestResult} requestResult
* @extends module:errors~FaunaHTTPError
* @constructor
*/
function BadRequest(requestResult) {
FaunaHTTPError.call(this, 'BadRequest', requestResult)
}
util.inherits(BadRequest, FaunaHTTPError)
/**
* A HTTP 401 error.
* @param {RequestResult} requestResult
* @extends module:errors~FaunaHTTPError
* @constructor
*/
function Unauthorized(requestResult) {
FaunaHTTPError.call(this, 'Unauthorized', requestResult)
}
util.inherits(Unauthorized, FaunaHTTPError)
/**
* A HTTP 403 error.
* @param {RequestResult} requestResult
* @extends module:errors~FaunaHTTPError
* @constructor
*/
function PermissionDenied(requestResult) {
FaunaHTTPError.call(this, 'PermissionDenied', requestResult)
}
util.inherits(PermissionDenied, FaunaHTTPError)
/**
* A HTTP 404 error.
* @param {RequestResult} requestResult
* @extends module:errors~FaunaHTTPError
* @constructor
*/
function NotFound(requestResult) {
FaunaHTTPError.call(this, 'NotFound', requestResult)
}
util.inherits(NotFound, FaunaHTTPError)
/**
* A HTTP 405 error.
* @param {RequestResult} requestResult
* @extends module:errors~FaunaHTTPError
* @constructor
*/
function MethodNotAllowed(requestResult) {
FaunaHTTPError.call(this, 'MethodNotAllowed', requestResult)
}
util.inherits(MethodNotAllowed, FaunaHTTPError)
/**
* A HTTP 429 error.
* @param {RequestResult} requestResult
* @extends module:errors~FaunaHTTPError
* @constructor
*/
function TooManyRequests(requestResult) {
FaunaHTTPError.call(this, 'TooManyRequests', requestResult)
}
util.inherits(TooManyRequests, FaunaHTTPError)
/**
* A HTTP 500 error.
* @param {RequestResult} requestResult
* @extends module:errors~FaunaHTTPError
* @constructor
*/
function InternalError(requestResult) {
FaunaHTTPError.call(this, 'InternalError', requestResult)
}
util.inherits(InternalError, FaunaHTTPError)
/**
* A HTTP 503 error.
* @param {RequestResult} requestResult
* @extends module:errors~FaunaHTTPError
* @constructor
*/
function UnavailableError(requestResult) {
FaunaHTTPError.call(this, 'UnavailableError', requestResult)
}
util.inherits(UnavailableError, FaunaHTTPError)
/**
* The base exception type for all stream related errors.
*
* @constructor
* @param {string} name The error class name.
* @param {string} message The error message.
* @param {string} description The error detailed description.
* @extends module:errors~FaunaError
*/
function StreamError(name, message, description) {
FaunaError.call(this, name, message, description)
}
util.inherits(StreamError, FaunaError)
/**
* An error thrown by the client when streams are not supported by the current
* platform.
*
* @constructor
* @param {string} description The error description.
* @extends module:errors~StreamError
*/
function StreamsNotSupported(description) {
FaunaError.call(
this,
'StreamsNotSupported',
'streams not supported',
description
)
}
util.inherits(StreamsNotSupported, StreamError)
/**
* An Error thrown by the server when something wrong happened with the
* subscribed stream.
* @constructor
* @param {Object} event The error event.
* @property {Object} event The error event.
* @extends module:errors~StreamError
*/
function StreamErrorEvent(event) {
var error = event.data || {}
FaunaError.call(this, 'StreamErrorEvent', error.code, error.description)
this.event = event
}
util.inherits(StreamErrorEvent, StreamError)
/**
* An error thrown when attempting to operate on a closed Client instance.
*
* @param {string} message The error message.
* @param {?string} description The error description.
* @extends module:errors~FaunaError
* @constructor
*/
function ClientClosed(message, description) {
FaunaError.call(this, 'ClientClosed', message, description)
}
util.inherits(ClientClosed, FaunaError)
module.exports = {
FaunaError: FaunaError,
ClientClosed: ClientClosed,
FaunaHTTPError: FaunaHTTPError,
InvalidValue: InvalidValue,
InvalidArity: InvalidArity,
BadRequest: BadRequest,
Unauthorized: Unauthorized,
PermissionDenied: PermissionDenied,
NotFound: NotFound,
MethodNotAllowed: MethodNotAllowed,
TooManyRequests: TooManyRequests,
InternalError: InternalError,
UnavailableError: UnavailableError,
StreamError: StreamError,
StreamsNotSupported: StreamsNotSupported,
StreamErrorEvent: StreamErrorEvent,
}