-
Notifications
You must be signed in to change notification settings - Fork 7
/
errors.ts
380 lines (340 loc) · 10.5 KB
/
errors.ts
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import type {
ConstraintFailure,
QueryFailure,
QueryInfo,
QueryStats,
QueryValue,
} from "./wire-protocol";
/**
* A common error base class for all other errors.
*/
export abstract class FaunaError extends Error {
constructor(...args: any[]) {
super(...args);
}
}
/**
* An error representing a query failure returned by Fauna.
*/
export class ServiceError extends FaunaError {
/**
* The HTTP Status Code of the error.
*/
readonly httpStatus?: number;
/**
* A code for the error. Codes indicate the cause of the error.
* It is safe to write programmatic logic against the code. They are
* part of the API contract.
*/
readonly code: string;
/**
* Details about the query sent along with the response
*/
readonly queryInfo?: QueryInfo;
/**
* A machine readable description of any constraint failures encountered by the query.
* Present only if this query encountered constraint failures.
*/
readonly constraint_failures?: Array<ConstraintFailure>;
constructor(failure: QueryFailure, httpStatus?: number) {
super(failure.error.message);
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ServiceError);
}
this.name = "ServiceError";
this.code = failure.error.code;
this.httpStatus = httpStatus;
const info: QueryInfo = {
txn_ts: failure.txn_ts,
summary: failure.summary,
query_tags: failure.query_tags,
stats: failure.stats,
};
this.queryInfo = info;
this.constraint_failures = failure.error.constraint_failures;
}
}
/**
* An error response that is the result of the query failing during execution.
* QueryRuntimeError's occur when a bug in your query causes an invalid execution
* to be requested.
* The 'code' field will vary based on the specific error cause.
*/
export class QueryRuntimeError extends ServiceError {
constructor(failure: QueryFailure, httpStatus?: number) {
super(failure, httpStatus);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, QueryRuntimeError);
}
this.name = "QueryRuntimeError";
// TODO trace, txn_ts, and stats not yet returned for QueryRuntimeError
// flip to check for those rather than a specific code.
}
}
/**
* An error due to a "compile-time" check of the query
* failing.
*/
export class QueryCheckError extends ServiceError {
constructor(failure: QueryFailure, httpStatus?: number) {
super(failure, httpStatus);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, QueryCheckError);
}
this.name = "QueryCheckError";
}
}
/**
* An error due to an invalid request to Fauna. Either the request body was not
* valid JSON or did not conform to the API specification
*/
export class InvalidRequestError extends ServiceError {
constructor(failure: QueryFailure, httpStatus?: number) {
super(failure, httpStatus);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, InvalidRequestError);
}
this.name = "InvalidRequestError";
}
}
/**
* A runtime error due to failing schema constraints.
*/
export class ConstraintFailureError extends ServiceError {
/**
* The list of constraints that failed.
*/
readonly constraint_failures: Array<ConstraintFailure>;
constructor(
failure: QueryFailure & {
error: { constraint_failures: Array<ConstraintFailure> };
},
httpStatus?: number,
) {
super(failure, httpStatus);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, QueryCheckError);
}
this.name = "ConstraintFailureError";
this.constraint_failures = failure.error.constraint_failures;
}
}
/**
* An error due to calling the FQL `abort` function.
*/
export class AbortError extends ServiceError {
/**
* The user provided value passed to the originating `abort()` call.
* Present only when the query encountered an `abort()` call, which is denoted
* by the error code `"abort"`
*/
readonly abort: QueryValue;
constructor(
failure: QueryFailure & { error: { abort: QueryValue } },
httpStatus?: number,
) {
super(failure, httpStatus);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, QueryCheckError);
}
this.name = "AbortError";
this.abort = failure.error.abort;
}
}
/**
* AuthenticationError indicates invalid credentials were
* used.
*/
export class AuthenticationError extends ServiceError {
constructor(failure: QueryFailure, httpStatus?: number) {
super(failure, httpStatus);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, AuthenticationError);
}
this.name = "AuthenticationError";
}
}
/**
* AuthorizationError indicates the credentials used do not have
* permission to perform the requested action.
*/
export class AuthorizationError extends ServiceError {
constructor(failure: QueryFailure, httpStatus?: number) {
super(failure, httpStatus);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, AuthorizationError);
}
this.name = "AuthorizationError";
}
}
/**
* An error due to a contended transaction.
*/
export class ContendedTransactionError extends ServiceError {
constructor(failure: QueryFailure, httpStatus?: number) {
super(failure, httpStatus);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, InvalidRequestError);
}
this.name = "ContendedTransactionError";
}
}
/**
* ThrottlingError indicates some capacity limit was exceeded
* and thus the request could not be served.
*/
export class ThrottlingError extends ServiceError {
constructor(failure: QueryFailure, httpStatus?: number) {
super(failure, httpStatus);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ThrottlingError);
}
this.name = "ThrottlingError";
}
}
/**
* A failure due to the query timeout being exceeded.
*
* This error can have one of two sources:
* 1. Fauna is behaving expectedly, but the query timeout provided was too
* aggressive and lower than the query's expected processing time.
* 2. Fauna was not available to service the request before the timeout was
* reached.
*
* In either case, consider increasing the `query_timeout_ms` configuration for
* your client.
*/
export class QueryTimeoutError extends ServiceError {
/**
* Statistics regarding the query.
*
* TODO: Deprecate this `stats` field. All `ServiceError`s already provide
* access to stats through `queryInfo.stats`
*/
readonly stats?: QueryStats;
constructor(failure: QueryFailure, httpStatus?: number) {
super(failure, httpStatus);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, QueryTimeoutError);
}
this.name = "QueryTimeoutError";
this.stats = failure.stats;
}
}
/**
* ServiceInternalError indicates Fauna failed unexpectedly.
*/
export class ServiceInternalError extends ServiceError {
constructor(failure: QueryFailure, httpStatus?: number) {
super(failure, httpStatus);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ServiceInternalError);
}
this.name = "ServiceInternalError";
}
}
/**
* An error representing a failure internal to the client, itself.
* This indicates Fauna was never called - the client failed internally
* prior to sending the request.
*/
export class ClientError extends FaunaError {
constructor(message: string, options?: { cause: any }) {
super(message, options);
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ClientError);
}
this.name = "ClientError";
}
}
/**
* An error thrown if you try to call the client after it has been closed.
*/
export class ClientClosedError extends FaunaError {
constructor(message: string, options?: { cause: any }) {
super(message, options);
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ClientClosedError);
}
this.name = "ClientClosedError";
}
}
/**
* An error representing a failure due to the network.
* This indicates Fauna was never reached.
*/
export class NetworkError extends FaunaError {
constructor(message: string, options: { cause: any }) {
super(message, options);
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, NetworkError);
}
this.name = "NetworkError";
}
}
/**
* An error representing a HTTP failure - but one not directly
* emitted by Fauna.
*/
export class ProtocolError extends FaunaError {
/**
* The HTTP Status Code of the error.
*/
readonly httpStatus: number;
constructor(error: { message: string; httpStatus: number }) {
super(error.message);
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ProtocolError);
}
this.name = "ProtocolError";
this.httpStatus = error.httpStatus;
}
}
export const getServiceError = (
failure: QueryFailure,
httpStatus?: number,
): ServiceError => {
const failureCode = failure.error.code;
switch (failureCode) {
case "invalid_query":
return new QueryCheckError(failure, httpStatus);
case "invalid_request":
return new InvalidRequestError(failure, httpStatus);
case "abort":
if (failure.error.abort !== undefined) {
return new AbortError(
failure as QueryFailure & { error: { abort: QueryValue } },
httpStatus,
);
}
break;
case "constraint_failure":
if (failure.error.constraint_failures !== undefined) {
return new ConstraintFailureError(
failure as QueryFailure & {
error: { constraint_failures: Array<ConstraintFailure> };
},
httpStatus,
);
}
break;
case "unauthorized":
return new AuthenticationError(failure, httpStatus);
case "forbidden":
return new AuthorizationError(failure, httpStatus);
case "contended_transaction":
return new ContendedTransactionError(failure, httpStatus);
case "limit_exceeded":
return new ThrottlingError(failure, httpStatus);
case "time_out":
return new QueryTimeoutError(failure, httpStatus);
case "internal_error":
return new ServiceInternalError(failure, httpStatus);
}
return new QueryRuntimeError(failure, httpStatus);
};