@fxhash/errors 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,2 +1,442 @@
1
- export * from "./errors/_index.js";
2
- export * from "./utils/_index.js";
1
+ import { IEquatableError, Result } from "@fxhash/utils";
2
+ import { CombinedError, OperationResult } from "@urql/core";
3
+
4
+ //#region src/errors/common.d.ts
5
+
6
+ /**
7
+ * Rich Error Messages are messages with extra data for a better usage of errors
8
+ * passed through the stack. Traditionally, error messages are intended for
9
+ * developers only (and some custom implementation for user error feedback is
10
+ * required for clean UIs).
11
+ */
12
+ interface IRichErrorMessages {
13
+ dev?: string;
14
+ user?: string;
15
+ }
16
+ /**
17
+ * Static error messages for "unexpected" errors. This payload is reused accross
18
+ * the stack for when error data is missing.
19
+ */
20
+ declare const UnexpectedRichErrorMessages: Readonly<Required<IRichErrorMessages>>;
21
+ /**
22
+ * Flatenned Rich Error interface so that Rich Errors can also be passed in a
23
+ * more convenient way. The purpose of this interface is to provide back-
24
+ * compatibility with `Error`, as well as providing an unambiguous `userMessage`
25
+ * property which can be used by front-end applications.
26
+ */
27
+ interface IRichError {
28
+ /**
29
+ * Message intended for developers
30
+ */
31
+ message: string;
32
+ /**
33
+ * Message intended for users
34
+ */
35
+ userMessage: string;
36
+ }
37
+ /**
38
+ * An error which provides messages intended for devs & users. This class should
39
+ * be used accross our stack to provide meaningful error objects which can be
40
+ * used for both developers & users. When typed errors are returned, if they all
41
+ * are instances of `RichError`, then client applications can take some simple
42
+ * shortcuts to provide error feedback to both the developer and the user.
43
+ *
44
+ * @example
45
+ *
46
+ * ```ts
47
+ * export class SomeCustomError extends RichError {
48
+ * name = "SomeCustomError" as const // `code` will have same value
49
+ * messages = {
50
+ * dev: "some message for devs",
51
+ * user: "some message for users"
52
+ * }
53
+ * }
54
+ * ```
55
+ *
56
+ * ```ts
57
+ * const res = await something()
58
+ * if (res.isFailure()) {
59
+ * const err = res.error // if this is typed as `RichError`
60
+ * displayErrorOnUI(err.userMessage) // safely display user message
61
+ * }
62
+ * ```
63
+ */
64
+ declare class RichError extends Error implements IRichError, IEquatableError {
65
+ messages: IRichErrorMessages;
66
+ messagesOverride: IRichErrorMessages | undefined;
67
+ constructor(messagesOverride?: IRichErrorMessages);
68
+ private _message;
69
+ get message(): string;
70
+ get userMessage(): string;
71
+ get code(): string;
72
+ serialize(): IRichErrorSerialized;
73
+ static get code(): string;
74
+ /**
75
+ * Instanciates a Rich Error, trying to match the serialized error with the
76
+ * provided Rich Error classes. The `code` property of the serialized payload
77
+ * is matched against `RichError.name`
78
+ *
79
+ * @param serialized A rich error serialized
80
+ * @param expected A list of Rich Error classes which are expected. If the
81
+ * serialized error doesn't match any of these, UnexpectedRichError will be
82
+ * returned.
83
+ *
84
+ * @returns An instance of Rich Error which type matches the `code` property,
85
+ * or {@link UnexpectedRichError} if no match.
86
+ */
87
+ static parse<T extends (typeof RichError)[]>(serialized: IRichErrorSerialized, expected: T): RichErrorUnion<T> | UnexpectedRichError;
88
+ /**
89
+ * Returns a new instance of {@link UnexpectedRichError}
90
+ * @param messagesOverride Optional overrides of default unexpected messages
91
+ */
92
+ static Unexpected(messagesOverride?: IRichErrorMessages): UnexpectedRichError;
93
+ }
94
+ /**
95
+ * A Rich error serialized,
96
+ */
97
+ interface IRichErrorSerialized {
98
+ code: string;
99
+ messages?: IRichErrorMessages;
100
+ }
101
+ /**
102
+ * A general-purpose error which is thrown when no better error could be
103
+ * inferred from the available context.
104
+ */
105
+ declare class UnexpectedRichError extends RichError {
106
+ name: "UnexpectedRichError";
107
+ messages: typeof UnexpectedRichErrorMessages;
108
+ }
109
+ /**
110
+ * Creates an Union of Rich Error instance types from an array of Rich Error
111
+ * classes.
112
+ */
113
+ type RichErrorUnion<T extends (typeof RichError)[]> = InstanceType<T[number]>;
114
+ //#endregion
115
+ //#region src/errors/network.d.ts
116
+ declare class NetworkRichError extends RichError {
117
+ name: "NetworkRichError";
118
+ messages: {
119
+ dev: string;
120
+ user: string;
121
+ };
122
+ }
123
+ //#endregion
124
+ //#region src/errors/graphql/common.d.ts
125
+ /**
126
+ * Format of the GraphQL error extensions returned by the GraphQL API.
127
+ */
128
+ interface IFxhashGraphQLErrorExtensions {
129
+ version: "fxhash@0.1.0";
130
+ richError: IRichErrorSerialized;
131
+ }
132
+ declare const GraphQLErrors: (typeof UnexpectedRichError | typeof NetworkRichError)[];
133
+ type WithGqlErrors<T extends RichError> = T | RichErrorUnion<typeof GraphQLErrors>;
134
+ //#endregion
135
+ //#region src/errors/graphql/email-otp.d.ts
136
+ declare class EmailOTPLockedError extends RichError {
137
+ name: "EmailOTPLockedError";
138
+ messages: {
139
+ dev: string;
140
+ user: string;
141
+ };
142
+ }
143
+ declare const EmailOTPRequestErrors: (typeof EmailOTPLockedError)[];
144
+ type EmailOTPRequestError = RichErrorUnion<typeof EmailOTPRequestErrors>;
145
+ declare class EmailOTPInvalidError extends RichError {
146
+ name: "EmailOTPInvalidError";
147
+ messages: {
148
+ dev: string;
149
+ user: string;
150
+ };
151
+ }
152
+ declare class EmailOTPExpiredError extends RichError {
153
+ name: "EmailOTPExpiredError";
154
+ messages: {
155
+ dev: string;
156
+ user: string;
157
+ };
158
+ }
159
+ declare const EmailOTPVerificationErrors: (typeof EmailOTPLockedError | typeof EmailOTPInvalidError | typeof EmailOTPExpiredError)[];
160
+ type EmailOTPVerificationError = RichErrorUnion<typeof EmailOTPVerificationErrors>;
161
+ //#endregion
162
+ //#region src/errors/graphql/oauth.d.ts
163
+ type OAuthProvider = "google" | "discord";
164
+ declare class OAuthTokenVerificationError extends RichError {
165
+ name: "OAuthTokenVerificationError";
166
+ constructor(messages: IRichErrorMessages);
167
+ constructor(provider: OAuthProvider);
168
+ }
169
+ declare class OAuthMissingInfoError extends RichError {
170
+ name: "OAuthMissingInfoError";
171
+ constructor(messages: IRichErrorMessages);
172
+ constructor(provider: OAuthProvider, missing: string[]);
173
+ }
174
+ declare const OAuthErrors: (typeof OAuthTokenVerificationError | typeof OAuthMissingInfoError)[];
175
+ type OAuthError = RichErrorUnion<typeof OAuthErrors>;
176
+ //#endregion
177
+ //#region src/errors/graphql/wallet-linking.d.ts
178
+ declare class WalletAlreadyOtherAccountMainWalletError extends RichError {
179
+ static readonly errorType = "WalletAlreadyOtherAccountMainWalletError";
180
+ name: "WalletAlreadyOtherAccountMainWalletError";
181
+ messages: {
182
+ dev: string;
183
+ user: string;
184
+ };
185
+ }
186
+ declare class WalletAlreadyLinkedError extends RichError {
187
+ static readonly errorType = "WalletAlreadyLinkedError";
188
+ name: "WalletAlreadyLinkedError";
189
+ messages: {
190
+ dev: string;
191
+ user: string;
192
+ };
193
+ }
194
+ declare class AccountAlreadyLinkedOnNetworkError extends RichError {
195
+ name: "AccountAlreadyLinkedOnNetworkError";
196
+ constructor(messages: IRichErrorMessages);
197
+ constructor(network: string);
198
+ }
199
+ declare const LinkWalletErrors: (typeof WalletAlreadyOtherAccountMainWalletError | typeof WalletAlreadyLinkedError | typeof AccountAlreadyLinkedOnNetworkError)[];
200
+ type LinkWalletError = RichErrorUnion<typeof LinkWalletErrors>;
201
+ declare class WalletNotLinkedToAccountError extends RichError {
202
+ name: "WalletNotLinkedToAccountError";
203
+ messages: {
204
+ dev: string;
205
+ user: string;
206
+ };
207
+ }
208
+ declare class MainWalletCannotBeUnlinkedError extends RichError {
209
+ name: "MainWalletCannotBeUnlinkedError";
210
+ messages: {
211
+ dev: string;
212
+ user: string;
213
+ };
214
+ }
215
+ declare const UnlinkWalletErrors: (typeof WalletNotLinkedToAccountError | typeof MainWalletCannotBeUnlinkedError)[];
216
+ type UnlinkWalletError = RichErrorUnion<typeof UnlinkWalletErrors>;
217
+ //#endregion
218
+ //#region src/errors/core.d.ts
219
+ declare class WalletSourceRequestConnectionRejectedError extends RichError {
220
+ name: "WalletSourceRequestConnectionRejectedError";
221
+ messages: {
222
+ dev: string;
223
+ user: string;
224
+ };
225
+ }
226
+ declare class WalletSourceRequestConnectionUnknownError extends RichError {
227
+ name: "WalletSourceRequestConnectionUnknownError";
228
+ constructor(messages: IRichErrorMessages);
229
+ constructor(network: string);
230
+ }
231
+ declare class WalletSourceRequestConnectionWalletNotAvailableError extends RichError {
232
+ name: "WalletSourceRequestConnectionWalletNotAvailableError";
233
+ constructor(messages: IRichErrorMessages);
234
+ constructor(network: string);
235
+ }
236
+ declare const WalletSourceRequestConnectionErrors: (typeof WalletSourceRequestConnectionRejectedError | typeof WalletSourceRequestConnectionUnknownError | typeof WalletSourceRequestConnectionWalletNotAvailableError)[];
237
+ type WalletSourceRequestConnectionError = RichErrorUnion<typeof WalletSourceRequestConnectionErrors>;
238
+ declare class NoWalletConnectedForNetworkError extends RichError {
239
+ name: "NoWalletConnectedForNetworkError";
240
+ constructor(messages: IRichErrorMessages);
241
+ constructor(network: string);
242
+ }
243
+ //#endregion
244
+ //#region src/errors/wallet-api.d.ts
245
+ declare class WalletAPIRpcUnknownNetworkError extends RichError {
246
+ name: "WalletApiRpcHealthError";
247
+ constructor(messages: IRichErrorMessages);
248
+ constructor(network: string);
249
+ }
250
+ declare class WalletAPIInvalidRequestError extends RichError {
251
+ name: "WalletAPIInvalidRequestError";
252
+ constructor(messages: IRichErrorMessages);
253
+ constructor(validationErrors?: Array<{
254
+ path: (string | number)[];
255
+ message: string;
256
+ }>);
257
+ }
258
+ declare class WalletAPIFetchError extends RichError {
259
+ name: "WalletAPIFetchError";
260
+ constructor(messages: IRichErrorMessages);
261
+ constructor(url: string, statusCode: number, attempt: number, maxRetries: number);
262
+ }
263
+ declare class WalletAPIFetchTimeoutError extends RichError {
264
+ name: "WalletAPIFetchTimeoutError";
265
+ constructor(messages: IRichErrorMessages);
266
+ constructor(url: string, maxRetries: number, finalError: unknown);
267
+ }
268
+ declare class WalletAPIInvalidURLError extends RichError {
269
+ name: "WalletAPIInvalidURLError";
270
+ constructor(messages: IRichErrorMessages);
271
+ constructor(url: string);
272
+ }
273
+ declare const WalletAPIErrors: (typeof WalletAPIRpcUnknownNetworkError | typeof WalletAPIInvalidRequestError | typeof WalletAPIFetchError | typeof WalletAPIFetchTimeoutError | typeof WalletAPIInvalidURLError)[];
274
+ type WalletAPIError = RichErrorUnion<typeof WalletAPIErrors>;
275
+ //#endregion
276
+ //#region src/errors/web3auth.d.ts
277
+ declare class Web3AuthFrameNotLoading extends RichError {
278
+ name: "Web3AuthFrameNotLoading";
279
+ constructor(url: string, error: string | Event);
280
+ constructor(messages: IRichErrorMessages);
281
+ }
282
+ declare class Web3AuthFrameNotResponding extends RichError {
283
+ name: "Web3AuthFrameNotResponding";
284
+ constructor(url: string);
285
+ }
286
+ declare class Web3AuthFrameNotInitialized extends RichError {
287
+ name: "Web3AuthFrameNotInitialized";
288
+ messages: {
289
+ dev: string;
290
+ user: string;
291
+ };
292
+ }
293
+ type Web3AuthFrameInitializationError = Web3AuthFrameNotInitialized | Web3AuthFrameNotLoading | Web3AuthFrameNotResponding;
294
+ declare class Web3AuthFrameAuthenticationError extends RichError {
295
+ name: "Web3AuthFrameAuthenticationError";
296
+ messages: {
297
+ dev: string;
298
+ user: string;
299
+ };
300
+ }
301
+ declare class Web3AuthFrameFxhashAuthenticationError extends RichError {
302
+ name: "Web3AuthFrameFxhashAuthenticationError";
303
+ messages: {
304
+ dev: string;
305
+ user: string;
306
+ };
307
+ }
308
+ declare class Web3AuthFrameLogoutFailedError extends RichError {
309
+ name: "Web3AuthFrameLogoutFailedError";
310
+ messages: {
311
+ dev: string;
312
+ user: string;
313
+ };
314
+ }
315
+ declare class Web3AuthInitializationFailedError extends RichError {
316
+ name: "Web3AuthInitializationFailedError";
317
+ messages: {
318
+ dev: string;
319
+ user: string;
320
+ };
321
+ }
322
+ declare class Web3AuthFrameUnknownError extends RichError {
323
+ name: "Web3AuthFrameUnknownError";
324
+ messages: {
325
+ dev: string;
326
+ user: string;
327
+ };
328
+ }
329
+ declare const Web3AuthFrameErrors: {
330
+ init: (typeof Web3AuthInitializationFailedError)[];
331
+ getSessionDetails: (typeof Web3AuthFrameAuthenticationError)[];
332
+ login: (typeof Web3AuthFrameAuthenticationError | typeof Web3AuthFrameFxhashAuthenticationError | typeof Web3AuthFrameUnknownError)[];
333
+ logout: (typeof Web3AuthFrameLogoutFailedError)[];
334
+ };
335
+ type Web3AuthFrameError = { [K in keyof typeof Web3AuthFrameErrors]: RichErrorUnion<(typeof Web3AuthFrameErrors)[K]> };
336
+ type AllWeb3AuthFrameError = TypeOfRichError<Web3AuthFrameError[keyof Web3AuthFrameError]>;
337
+ declare const AllWeb3AuthFrameErrors: AllWeb3AuthFrameError[];
338
+ //#endregion
339
+ //#region src/utils/rich-error.d.ts
340
+ type TypeOfRichError<T extends RichError> = {
341
+ new (): T;
342
+ parse: (typeof RichError)["parse"];
343
+ Unexpected: (typeof RichError)["Unexpected"];
344
+ code: (typeof RichError)["code"];
345
+ };
346
+ /**
347
+ * Instanciate a new {@link RichError} using a declarative object. This is
348
+ * useful when Rich Error are instanciated programmatically when type is
349
+ * unknown.
350
+ */
351
+ declare function richError(params: {
352
+ name: string;
353
+ messages?: IRichErrorMessages;
354
+ }): RichError;
355
+ declare function isFxhashErrorExtensions(ext: any): ext is IFxhashGraphQLErrorExtensions;
356
+ /**
357
+ * Test whether a given value is implementing the {@link IRichErrorMessages}
358
+ * interface.
359
+ * @param value Any value
360
+ */
361
+ declare function isRichErrorMessages(value: any): value is IRichErrorMessages;
362
+ /**
363
+ * Parses the GraphQL error object into a RichError. This function detects
364
+ * fxhash error extensions for outputting user error messages returned by
365
+ * the backend.
366
+ *
367
+ * @param error A GraphQL error response
368
+ *
369
+ * @returns An "untyped" rich error constructed by parsing the GraphQL error
370
+ */
371
+ declare function richErrorFromGraphQLError(error: CombinedError): RichError | UnexpectedRichError | NetworkRichError;
372
+ /**
373
+ * Parses the GraphQL error response to find the fxhash GraphQL error extension,
374
+ * which is used to instanciate a Rich Error from a list of provided RichErrors.
375
+ * The `name` constant property of such classes will be compared to the `code`
376
+ * property of the fxhash error extension to find a match.
377
+ *
378
+ * @param graphQLError GraphQL error response
379
+ * @param expectedErrors An array of RichError classes which will be parsed to
380
+ * find matches between the RichError `name` constant and the `code` returned
381
+ * by the GraphQL fxhash error extension.
382
+ *
383
+ * @returns A RichError instance matchin the error code, or
384
+ * {@link NetworkRichError} if a network error occured, else
385
+ * {@link UnexpectedRichError}
386
+ */
387
+ declare function typedRichErrorFromGraphQLError<T extends (typeof RichError)[]>(graphQLError: CombinedError, expectedErrors: T): WithGqlErrors<InstanceType<T[number]>>;
388
+ /**
389
+ * Returns a `Result<Data, TypedRichError>` by parsing a GraphQL response. If
390
+ * the response has an error, {@link typedRichErrorFromGraphQLError} will be
391
+ * called with such error to return a proper error instance based on the error
392
+ * code in the fxhash graphql error extension (or a a generic error if none).
393
+ *
394
+ * @param operationResult A GraphQL response from fxhash hasura endpoint
395
+ * @param getData A function which takes the response and returns the data (if
396
+ * no data is found it should return `null` | `undefined`, in which case it will
397
+ * fail with UnexpectedError)
398
+ * @param potentialErrors An array of Rich Error classes which could be found in
399
+ * the error response.
400
+ *
401
+ * @example
402
+ *
403
+ * ```ts
404
+ * const emailRequestOTP = async (email) => {
405
+ * return richResultFromGraphQLResponse(
406
+ * await gqlWrapper.client().mutation(Mu_Web3AuthEmailRequestOTP, {
407
+ * email,
408
+ * }),
409
+ * res => res.data?.web3auth_email_request_otp,
410
+ * EmailOTPRequestErrors
411
+ * )
412
+ * },
413
+ * ```
414
+ */
415
+ declare function richResultFromGraphQLResponse<T extends (typeof RichError)[], Data = any, ExtractedData = any>(operationResult: OperationResult<Data>, getData: (result: OperationResult<Data>) => ExtractedData | undefined | null, potentialErrors: T): Result<ExtractedData, WithGqlErrors<InstanceType<T[number]>>>;
416
+ /**
417
+ * Test if an error is of a certain error kind, among a list of [errors] or
418
+ * [list of errors]. This allows testing multiple array of errors, which are
419
+ * defined quite a lot throughout the errors stack.
420
+ *
421
+ * @param error The error which needs to be tested
422
+ * @param kinds List of [errors]/[array of errors]
423
+ *
424
+ * @returns boolean if error is of given kind
425
+ *
426
+ * @example
427
+ *
428
+ * ```ts
429
+ * isErrorOfKind(
430
+ * someErr,
431
+ * UnexpectedRichError,
432
+ * [SuperError, BigError],
433
+ * SimpleError
434
+ * )
435
+ * ```
436
+ */
437
+ declare function isErrorOfKind<Errors extends (IEquatableError | IEquatableError[])[]>(error: IEquatableError, ...kinds: Errors): error is Instance<Flatten<Errors>>;
438
+ type Flatten<T> = T extends (infer U)[] ? Flatten<U> : T;
439
+ type Instance<T> = T extends (abstract new (...args: any) => any) ? InstanceType<T> : T;
440
+ //#endregion
441
+ export { AccountAlreadyLinkedOnNetworkError, AllWeb3AuthFrameError, AllWeb3AuthFrameErrors, EmailOTPExpiredError, EmailOTPInvalidError, EmailOTPLockedError, EmailOTPRequestError, EmailOTPRequestErrors, EmailOTPVerificationError, EmailOTPVerificationErrors, GraphQLErrors, IFxhashGraphQLErrorExtensions, IRichError, IRichErrorMessages, IRichErrorSerialized, LinkWalletError, LinkWalletErrors, MainWalletCannotBeUnlinkedError, NetworkRichError, NoWalletConnectedForNetworkError, OAuthError, OAuthErrors, OAuthMissingInfoError, OAuthTokenVerificationError, RichError, RichErrorUnion, TypeOfRichError, UnexpectedRichError, UnexpectedRichErrorMessages, UnlinkWalletError, UnlinkWalletErrors, WalletAPIError, WalletAPIErrors, WalletAPIFetchError, WalletAPIFetchTimeoutError, WalletAPIInvalidRequestError, WalletAPIInvalidURLError, WalletAPIRpcUnknownNetworkError, WalletAlreadyLinkedError, WalletAlreadyOtherAccountMainWalletError, WalletNotLinkedToAccountError, WalletSourceRequestConnectionError, WalletSourceRequestConnectionErrors, WalletSourceRequestConnectionRejectedError, WalletSourceRequestConnectionUnknownError, WalletSourceRequestConnectionWalletNotAvailableError, Web3AuthFrameAuthenticationError, Web3AuthFrameError, Web3AuthFrameErrors, Web3AuthFrameFxhashAuthenticationError, Web3AuthFrameInitializationError, Web3AuthFrameLogoutFailedError, Web3AuthFrameNotInitialized, Web3AuthFrameNotLoading, Web3AuthFrameNotResponding, Web3AuthFrameUnknownError, Web3AuthInitializationFailedError, WithGqlErrors, isErrorOfKind, isFxhashErrorExtensions, isRichErrorMessages, richError, richErrorFromGraphQLError, richResultFromGraphQLResponse, typedRichErrorFromGraphQLError };
442
+ //# sourceMappingURL=index.d.ts.map