@fxhash/errors 0.0.10

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/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @fxhash/errors
2
+
3
+ Application-wide errors which are exposed and used by different parts of our stack.
@@ -0,0 +1,6 @@
1
+ export * from "./common.js";
2
+ export * from "./graphql/_index.js";
3
+ export * from "./core.js";
4
+ export * from "./network.js";
5
+ export * from "./wallet-api.js";
6
+ export * from "./web3auth.js";
@@ -0,0 +1,109 @@
1
+ import type { IEquatableError } from "@fxhash/utils";
2
+ /**
3
+ * Rich Error Messages are messages with extra data for a better usage of errors
4
+ * passed through the stack. Traditionally, error messages are intended for
5
+ * developers only (and some custom implementation for user error feedback is
6
+ * required for clean UIs).
7
+ */
8
+ export interface IRichErrorMessages {
9
+ dev?: string;
10
+ user?: string;
11
+ }
12
+ /**
13
+ * Static error messages for "unexpected" errors. This payload is reused accross
14
+ * the stack for when error data is missing.
15
+ */
16
+ export declare const UnexpectedRichErrorMessages: Readonly<Required<IRichErrorMessages>>;
17
+ /**
18
+ * Flatenned Rich Error interface so that Rich Errors can also be passed in a
19
+ * more convenient way. The purpose of this interface is to provide back-
20
+ * compatibility with `Error`, as well as providing an unambiguous `userMessage`
21
+ * property which can be used by front-end applications.
22
+ */
23
+ export interface IRichError {
24
+ /**
25
+ * Message intended for developers
26
+ */
27
+ message: string;
28
+ /**
29
+ * Message intended for users
30
+ */
31
+ userMessage: string;
32
+ }
33
+ /**
34
+ * An error which provides messages intended for devs & users. This class should
35
+ * be used accross our stack to provide meaningful error objects which can be
36
+ * used for both developers & users. When typed errors are returned, if they all
37
+ * are instances of `RichError`, then client applications can take some simple
38
+ * shortcuts to provide error feedback to both the developer and the user.
39
+ *
40
+ * @example
41
+ *
42
+ * ```ts
43
+ * export class SomeCustomError extends RichError {
44
+ * name = "SomeCustomError" as const // `code` will have same value
45
+ * messages = {
46
+ * dev: "some message for devs",
47
+ * user: "some message for users"
48
+ * }
49
+ * }
50
+ * ```
51
+ *
52
+ * ```ts
53
+ * const res = await something()
54
+ * if (res.isFailure()) {
55
+ * const err = res.error // if this is typed as `RichError`
56
+ * displayErrorOnUI(err.userMessage) // safely display user message
57
+ * }
58
+ * ```
59
+ */
60
+ export declare class RichError extends Error implements IRichError, IEquatableError {
61
+ messages: IRichErrorMessages;
62
+ messagesOverride: IRichErrorMessages | undefined;
63
+ constructor(messagesOverride?: IRichErrorMessages);
64
+ private _message;
65
+ get message(): string;
66
+ get userMessage(): string;
67
+ get code(): string;
68
+ serialize(): IRichErrorSerialized;
69
+ static get code(): string;
70
+ /**
71
+ * Instanciates a Rich Error, trying to match the serialized error with the
72
+ * provided Rich Error classes. The `code` property of the serialized payload
73
+ * is matched against `RichError.name`
74
+ *
75
+ * @param serialized A rich error serialized
76
+ * @param expected A list of Rich Error classes which are expected. If the
77
+ * serialized error doesn't match any of these, UnexpectedRichError will be
78
+ * returned.
79
+ *
80
+ * @returns An instance of Rich Error which type matches the `code` property,
81
+ * or {@link UnexpectedRichError} if no match.
82
+ */
83
+ static parse<T extends (typeof RichError)[]>(serialized: IRichErrorSerialized, expected: T): RichErrorUnion<T> | UnexpectedRichError;
84
+ /**
85
+ * Returns a new instance of {@link UnexpectedRichError}
86
+ * @param messagesOverride Optional overrides of default unexpected messages
87
+ */
88
+ static Unexpected(messagesOverride?: IRichErrorMessages): UnexpectedRichError;
89
+ }
90
+ /**
91
+ * A Rich error serialized,
92
+ */
93
+ export interface IRichErrorSerialized {
94
+ code: string;
95
+ messages?: IRichErrorMessages;
96
+ }
97
+ /**
98
+ * A general-purpose error which is thrown when no better error could be
99
+ * inferred from the available context.
100
+ */
101
+ export declare class UnexpectedRichError extends RichError {
102
+ name: "UnexpectedRichError";
103
+ messages: typeof UnexpectedRichErrorMessages;
104
+ }
105
+ /**
106
+ * Creates an Union of Rich Error instance types from an array of Rich Error
107
+ * classes.
108
+ */
109
+ export type RichErrorUnion<T extends (typeof RichError)[]> = InstanceType<T[number]>;
@@ -0,0 +1,25 @@
1
+ import { IRichErrorMessages, RichError, RichErrorUnion } from "./common.js";
2
+ export declare class WalletSourceRequestConnectionRejectedError extends RichError {
3
+ name: "WalletSourceRequestConnectionRejectedError";
4
+ messages: {
5
+ dev: string
6
+ user: string
7
+ };
8
+ }
9
+ export declare class WalletSourceRequestConnectionUnknownError extends RichError {
10
+ name: "WalletSourceRequestConnectionUnknownError";
11
+ constructor(messages: IRichErrorMessages);
12
+ constructor(network: string);
13
+ }
14
+ export declare class WalletSourceRequestConnectionWalletNotAvailableError extends RichError {
15
+ name: "WalletSourceRequestConnectionWalletNotAvailableError";
16
+ constructor(messages: IRichErrorMessages);
17
+ constructor(network: string);
18
+ }
19
+ export declare const WalletSourceRequestConnectionErrors: (typeof WalletSourceRequestConnectionRejectedError | typeof WalletSourceRequestConnectionUnknownError | typeof WalletSourceRequestConnectionWalletNotAvailableError)[];
20
+ export type WalletSourceRequestConnectionError = RichErrorUnion<typeof WalletSourceRequestConnectionErrors>;
21
+ export declare class NoWalletConnectedForNetworkError extends RichError {
22
+ name: "NoWalletConnectedForNetworkError";
23
+ constructor(messages: IRichErrorMessages);
24
+ constructor(network: string);
25
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./common.js";
2
+ export * from "./email-otp.js";
3
+ export * from "./oauth.js";
4
+ export * from "./wallet-linking.js";
@@ -0,0 +1,11 @@
1
+ import { IRichErrorSerialized, RichError, RichErrorUnion, UnexpectedRichError } from "../common.js";
2
+ import { NetworkRichError } from "../network.js";
3
+ /**
4
+ * Format of the GraphQL error extensions returned by the GraphQL API.
5
+ */
6
+ export interface IFxhashGraphQLErrorExtensions {
7
+ version: "fxhash@0.1.0";
8
+ richError: IRichErrorSerialized;
9
+ }
10
+ export declare const GraphQLErrors: (typeof UnexpectedRichError | typeof NetworkRichError)[];
11
+ export type WithGqlErrors<T extends RichError> = T | RichErrorUnion<typeof GraphQLErrors>;
@@ -0,0 +1,26 @@
1
+ import { RichError, RichErrorUnion } from "../common.js";
2
+ export declare class EmailOTPLockedError extends RichError {
3
+ name: "EmailOTPLockedError";
4
+ messages: {
5
+ dev: string
6
+ user: string
7
+ };
8
+ }
9
+ export declare const EmailOTPRequestErrors: (typeof EmailOTPLockedError)[];
10
+ export type EmailOTPRequestError = RichErrorUnion<typeof EmailOTPRequestErrors>;
11
+ export declare class EmailOTPInvalidError extends RichError {
12
+ name: "EmailOTPInvalidError";
13
+ messages: {
14
+ dev: string
15
+ user: string
16
+ };
17
+ }
18
+ export declare class EmailOTPExpiredError extends RichError {
19
+ name: "EmailOTPExpiredError";
20
+ messages: {
21
+ dev: string
22
+ user: string
23
+ };
24
+ }
25
+ export declare const EmailOTPVerificationErrors: (typeof EmailOTPLockedError | typeof EmailOTPInvalidError | typeof EmailOTPExpiredError)[];
26
+ export type EmailOTPVerificationError = RichErrorUnion<typeof EmailOTPVerificationErrors>;
@@ -0,0 +1,15 @@
1
+ import { IRichErrorMessages, RichError, RichErrorUnion } from "../common.js";
2
+ type OAuthProvider = "google" | "discord";
3
+ export declare class OAuthTokenVerificationError extends RichError {
4
+ name: "OAuthTokenVerificationError";
5
+ constructor(messages: IRichErrorMessages);
6
+ constructor(provider: OAuthProvider);
7
+ }
8
+ export declare class OAuthMissingInfoError extends RichError {
9
+ name: "OAuthMissingInfoError";
10
+ constructor(messages: IRichErrorMessages);
11
+ constructor(provider: OAuthProvider, missing: string[]);
12
+ }
13
+ export declare const OAuthErrors: (typeof OAuthTokenVerificationError | typeof OAuthMissingInfoError)[];
14
+ export type OAuthError = RichErrorUnion<typeof OAuthErrors>;
15
+ export {};
@@ -0,0 +1,38 @@
1
+ import { IRichErrorMessages, RichError, RichErrorUnion } from "../common.js";
2
+ export declare class WalletAlreadyOtherAccountMainWalletError extends RichError {
3
+ name: "WalletAlreadyOtherAccountMainWalletError";
4
+ messages: {
5
+ dev: string
6
+ user: string
7
+ };
8
+ }
9
+ export declare class WalletAlreadyLinkedError extends RichError {
10
+ name: "WalletAlreadyLinkedError";
11
+ messages: {
12
+ dev: string
13
+ user: string
14
+ };
15
+ }
16
+ export declare class AccountAlreadyLinkedOnNetworkError extends RichError {
17
+ name: "AccountAlreadyLinkedOnNetworkError";
18
+ constructor(messages: IRichErrorMessages);
19
+ constructor(network: string);
20
+ }
21
+ export declare const LinkWalletErrors: (typeof WalletAlreadyOtherAccountMainWalletError | typeof WalletAlreadyLinkedError | typeof AccountAlreadyLinkedOnNetworkError)[];
22
+ export type LinkWalletError = RichErrorUnion<typeof LinkWalletErrors>;
23
+ export declare class WalletNotLinkedToAccountError extends RichError {
24
+ name: "WalletNotLinkedToAccountError";
25
+ messages: {
26
+ dev: string
27
+ user: string
28
+ };
29
+ }
30
+ export declare class MainWalletCannotBeUnlinkedError extends RichError {
31
+ name: "MainWalletCannotBeUnlinkedError";
32
+ messages: {
33
+ dev: string
34
+ user: string
35
+ };
36
+ }
37
+ export declare const UnlinkWalletErrors: (typeof WalletNotLinkedToAccountError | typeof MainWalletCannotBeUnlinkedError)[];
38
+ export type UnlinkWalletError = RichErrorUnion<typeof UnlinkWalletErrors>;
@@ -0,0 +1,8 @@
1
+ import { RichError } from "./common.js";
2
+ export declare class NetworkRichError extends RichError {
3
+ name: "NetworkRichError";
4
+ messages: {
5
+ dev: string
6
+ user: string
7
+ };
8
+ }
@@ -0,0 +1,31 @@
1
+ import { RichError, RichErrorUnion, IRichErrorMessages } from "./common.js";
2
+ export declare class WalletAPIRpcUnknownNetworkError extends RichError {
3
+ name: "WalletApiRpcHealthError";
4
+ constructor(messages: IRichErrorMessages);
5
+ constructor(network: string);
6
+ }
7
+ export declare class WalletAPIInvalidRequestError extends RichError {
8
+ name: "WalletAPIInvalidRequestError";
9
+ constructor(messages: IRichErrorMessages);
10
+ constructor(validationErrors?: Array<{
11
+ path: (string | number)[]
12
+ message: string
13
+ }>);
14
+ }
15
+ export declare class WalletAPIFetchError extends RichError {
16
+ name: "WalletAPIFetchError";
17
+ constructor(messages: IRichErrorMessages);
18
+ constructor(url: string, statusCode: number, attempt: number, maxRetries: number);
19
+ }
20
+ export declare class WalletAPIFetchTimeoutError extends RichError {
21
+ name: "WalletAPIFetchTimeoutError";
22
+ constructor(messages: IRichErrorMessages);
23
+ constructor(url: string, maxRetries: number, finalError: unknown);
24
+ }
25
+ export declare class WalletAPIInvalidURLError extends RichError {
26
+ name: "WalletAPIInvalidURLError";
27
+ constructor(messages: IRichErrorMessages);
28
+ constructor(url: string);
29
+ }
30
+ export declare const WalletAPIErrors: (typeof WalletAPIRpcUnknownNetworkError | typeof WalletAPIInvalidRequestError | typeof WalletAPIFetchError | typeof WalletAPIFetchTimeoutError | typeof WalletAPIInvalidURLError)[];
31
+ export type WalletAPIError = RichErrorUnion<typeof WalletAPIErrors>;
@@ -0,0 +1,63 @@
1
+ import { TypeOfRichError } from "../index.js";
2
+ import { IRichErrorMessages, RichError, RichErrorUnion } from "./common.js";
3
+ export declare class Web3AuthFrameNotLoading extends RichError {
4
+ name: "Web3AuthFrameNotLoading";
5
+ constructor(url: string, error: string | Event);
6
+ constructor(messages: IRichErrorMessages);
7
+ }
8
+ export declare class Web3AuthFrameNotResponding extends RichError {
9
+ name: "Web3AuthFrameNotResponding";
10
+ constructor(url: string);
11
+ }
12
+ export declare class Web3AuthFrameNotInitialized extends RichError {
13
+ name: "Web3AuthFrameNotInitialized";
14
+ messages: {
15
+ dev: string
16
+ user: string
17
+ };
18
+ }
19
+ export type Web3AuthFrameInitializationError = Web3AuthFrameNotInitialized | Web3AuthFrameNotLoading | Web3AuthFrameNotResponding;
20
+ export declare class Web3AuthFrameAuthenticationError extends RichError {
21
+ name: "Web3AuthFrameAuthenticationError";
22
+ messages: {
23
+ dev: string
24
+ user: string
25
+ };
26
+ }
27
+ export declare class Web3AuthFrameFxhashAuthenticationError extends RichError {
28
+ name: "Web3AuthFrameFxhashAuthenticationError";
29
+ messages: {
30
+ dev: string
31
+ user: string
32
+ };
33
+ }
34
+ export declare class Web3AuthFrameLogoutFailedError extends RichError {
35
+ name: "Web3AuthFrameLogoutFailedError";
36
+ messages: {
37
+ dev: string
38
+ user: string
39
+ };
40
+ }
41
+ export declare class Web3AuthInitializationFailedError extends RichError {
42
+ name: "Web3AuthInitializationFailedError";
43
+ messages: {
44
+ dev: string
45
+ user: string
46
+ };
47
+ }
48
+ export declare class Web3AuthFrameUnknownError extends RichError {
49
+ name: "Web3AuthFrameUnknownError";
50
+ messages: {
51
+ dev: string
52
+ user: string
53
+ };
54
+ }
55
+ export declare const Web3AuthFrameErrors: {
56
+ init: (typeof Web3AuthInitializationFailedError)[]
57
+ getSessionDetails: (typeof Web3AuthFrameAuthenticationError)[]
58
+ login: (typeof Web3AuthFrameAuthenticationError | typeof Web3AuthFrameFxhashAuthenticationError | typeof Web3AuthFrameUnknownError)[]
59
+ logout: (typeof Web3AuthFrameLogoutFailedError)[]
60
+ };
61
+ export type Web3AuthFrameError = { [K in keyof typeof Web3AuthFrameErrors] : RichErrorUnion<(typeof Web3AuthFrameErrors)[K]> };
62
+ export type AllWeb3AuthFrameError = TypeOfRichError<Web3AuthFrameError[keyof Web3AuthFrameError]>;
63
+ export declare const AllWeb3AuthFrameErrors: AllWeb3AuthFrameError[];
@@ -0,0 +1,2 @@
1
+ export * from "./errors/_index.js";
2
+ export * from "./utils/_index.js";
package/dist/index.js ADDED
@@ -0,0 +1,537 @@
1
+ // src/errors/common.ts
2
+ var UnexpectedRichErrorMessages = {
3
+ dev: "Unexpected error",
4
+ user: "Unexpected error"
5
+ };
6
+ var RichError = class extends Error {
7
+ constructor(messagesOverride) {
8
+ super();
9
+ this.messages = UnexpectedRichErrorMessages;
10
+ if (messagesOverride) {
11
+ this.messagesOverride = messagesOverride;
12
+ }
13
+ }
14
+ _message(target) {
15
+ return this.messagesOverride?.[target] || this.messages[target] || UnexpectedRichErrorMessages[target];
16
+ }
17
+ get message() {
18
+ return this._message("dev");
19
+ }
20
+ get userMessage() {
21
+ return this._message("user");
22
+ }
23
+ get code() {
24
+ return this.name;
25
+ }
26
+ serialize() {
27
+ return {
28
+ code: this.code,
29
+ messages: this.messagesOverride || this.messages
30
+ };
31
+ }
32
+ static get code() {
33
+ return this.name;
34
+ }
35
+ /**
36
+ * Instanciates a Rich Error, trying to match the serialized error with the
37
+ * provided Rich Error classes. The `code` property of the serialized payload
38
+ * is matched against `RichError.name`
39
+ *
40
+ * @param serialized A rich error serialized
41
+ * @param expected A list of Rich Error classes which are expected. If the
42
+ * serialized error doesn't match any of these, UnexpectedRichError will be
43
+ * returned.
44
+ *
45
+ * @returns An instance of Rich Error which type matches the `code` property,
46
+ * or {@link UnexpectedRichError} if no match.
47
+ */
48
+ static parse(serialized, expected) {
49
+ for (const RichErrorClass of expected) {
50
+ if (RichErrorClass.code === serialized.code) {
51
+ return new RichErrorClass(serialized.messages);
52
+ }
53
+ }
54
+ return new UnexpectedRichError(serialized.messages);
55
+ }
56
+ /**
57
+ * Returns a new instance of {@link UnexpectedRichError}
58
+ * @param messagesOverride Optional overrides of default unexpected messages
59
+ */
60
+ static Unexpected(messagesOverride) {
61
+ return new UnexpectedRichError(messagesOverride);
62
+ }
63
+ };
64
+ var UnexpectedRichError = class extends RichError {
65
+ constructor() {
66
+ super(...arguments);
67
+ this.name = "UnexpectedRichError";
68
+ this.messages = UnexpectedRichErrorMessages;
69
+ }
70
+ };
71
+
72
+ // src/errors/network.ts
73
+ var NetworkRichError = class extends RichError {
74
+ constructor() {
75
+ super(...arguments);
76
+ this.name = "NetworkRichError";
77
+ this.messages = {
78
+ dev: "An network error occured.",
79
+ user: "Network error"
80
+ };
81
+ }
82
+ };
83
+
84
+ // src/errors/graphql/common.ts
85
+ var GraphQLErrors = [NetworkRichError, UnexpectedRichError];
86
+
87
+ // src/errors/graphql/email-otp.ts
88
+ var EmailOTPLockedError = class extends RichError {
89
+ constructor() {
90
+ super(...arguments);
91
+ this.name = "EmailOTPLockedError";
92
+ this.messages = {
93
+ dev: "Email locked 2h because too many attempts",
94
+ user: "This email is locked for verification during 2 hours because of too many attempts in a short period of time"
95
+ };
96
+ }
97
+ };
98
+ var EmailOTPRequestErrors = [
99
+ EmailOTPLockedError
100
+ ];
101
+ var EmailOTPInvalidError = class extends RichError {
102
+ constructor() {
103
+ super(...arguments);
104
+ this.name = "EmailOTPInvalidError";
105
+ this.messages = {
106
+ dev: "Email verification failed because OTP is invalid",
107
+ user: "The validation code is invalid"
108
+ };
109
+ }
110
+ };
111
+ var EmailOTPExpiredError = class extends RichError {
112
+ constructor() {
113
+ super(...arguments);
114
+ this.name = "EmailOTPExpiredError";
115
+ this.messages = {
116
+ dev: "Email verification failed because OTP has expired. A new OTP request must be initiated",
117
+ user: "Verification code has expired. Please make another request."
118
+ };
119
+ }
120
+ };
121
+ var EmailOTPVerificationErrors = [EmailOTPInvalidError, EmailOTPExpiredError, EmailOTPLockedError];
122
+
123
+ // src/utils/rich-error.ts
124
+ import { failure, success } from "@fxhash/utils";
125
+ function richError(params) {
126
+ return Object.assign(new RichError(), params);
127
+ }
128
+ function isFxhashErrorExtensions(ext) {
129
+ return typeof ext === "object" && ext.version === "fxhash@0.1.0";
130
+ }
131
+ function isRichErrorMessages(value) {
132
+ if (typeof value !== "object") return false;
133
+ return typeof value.dev === "string" || typeof value.user === "string";
134
+ }
135
+ function richErrorFromGraphQLError(error) {
136
+ if (error.graphQLErrors.length > 0) {
137
+ const gqlError = error.graphQLErrors[0];
138
+ if (isFxhashErrorExtensions(gqlError.extensions)) {
139
+ return richError({
140
+ name: gqlError.extensions.richError.code,
141
+ messages: gqlError.extensions.richError.messages
142
+ });
143
+ }
144
+ return new UnexpectedRichError();
145
+ }
146
+ if (error.networkError) {
147
+ return new NetworkRichError();
148
+ }
149
+ return new UnexpectedRichError();
150
+ }
151
+ function typedRichErrorFromGraphQLError(graphQLError, expectedErrors) {
152
+ if (graphQLError.networkError) {
153
+ return new NetworkRichError();
154
+ }
155
+ if (graphQLError.graphQLErrors.length > 0) {
156
+ const gqlError = graphQLError.graphQLErrors[0];
157
+ if (isFxhashErrorExtensions(gqlError.extensions)) {
158
+ return RichError.parse(gqlError.extensions.richError, expectedErrors);
159
+ }
160
+ return new UnexpectedRichError();
161
+ }
162
+ return new UnexpectedRichError();
163
+ }
164
+ function richResultFromGraphQLResponse(operationResult, getData, potentialErrors) {
165
+ const res = operationResult;
166
+ if (res.error) {
167
+ return failure(typedRichErrorFromGraphQLError(res.error, potentialErrors));
168
+ }
169
+ const data = getData(res);
170
+ return data ? success(data) : failure(
171
+ new UnexpectedRichError({
172
+ dev: "Expected data missing from GraphQL response"
173
+ })
174
+ );
175
+ }
176
+ function isErrorOfKind(error, ...kinds) {
177
+ for (const kind of kinds) {
178
+ if (Array.isArray(kind)) {
179
+ if (isErrorOfKind(error, ...kind)) return true;
180
+ } else {
181
+ if (error.name === kind.name) return true;
182
+ }
183
+ }
184
+ return false;
185
+ }
186
+
187
+ // src/errors/graphql/oauth.ts
188
+ import { capitalize } from "@fxhash/utils";
189
+ var couldntSignIn = (provider) => `Couldn't sign in using ${capitalize(provider)}`;
190
+ var OAuthTokenVerificationError = class extends RichError {
191
+ constructor(par) {
192
+ super(
193
+ isRichErrorMessages(par) ? par : {
194
+ dev: `The provided ${capitalize(par)} OAuth token could not be verified against ${capitalize(par)} services.`,
195
+ user: couldntSignIn(par)
196
+ }
197
+ );
198
+ this.name = "OAuthTokenVerificationError";
199
+ }
200
+ };
201
+ var OAuthMissingInfoError = class extends RichError {
202
+ constructor(par, missing) {
203
+ super(
204
+ isRichErrorMessages(par) ? par : {
205
+ dev: `Some user information is missing at the end of the ${capitalize(par)} OAuth authentication flow: ${missing?.join(", ")}. This shouldn't happen and requires investigation from the fxhash team. Please forward this issue to our team.`,
206
+ user: couldntSignIn(par)
207
+ }
208
+ );
209
+ this.name = "OAuthMissingInfoError";
210
+ }
211
+ };
212
+ var OAuthErrors = [OAuthTokenVerificationError, OAuthMissingInfoError];
213
+
214
+ // src/errors/graphql/wallet-linking.ts
215
+ var WalletAlreadyOtherAccountMainWalletError = class extends RichError {
216
+ constructor() {
217
+ super(...arguments);
218
+ this.name = "WalletAlreadyOtherAccountMainWalletError";
219
+ this.messages = {
220
+ dev: "Wallet is already the main wallet of another account.",
221
+ user: "This wallet is already registered as the main wallet for another account. To link this wallet to your current account, you need to delete the account associated with this wallet first. Please log in to the other account and proceed to delete the account from the profile menu. After this, you can link the wallet to this account."
222
+ };
223
+ }
224
+ };
225
+ var WalletAlreadyLinkedError = class extends RichError {
226
+ constructor() {
227
+ super(...arguments);
228
+ this.name = "WalletAlreadyLinkedError";
229
+ this.messages = {
230
+ dev: "Wallet is already linked to another account (not as the main wallet)",
231
+ user: "This wallet is already associated to another account. You must first connect to your other account and unlink this wallet from it."
232
+ };
233
+ }
234
+ };
235
+ var AccountAlreadyLinkedOnNetworkError = class extends RichError {
236
+ constructor(par) {
237
+ super(
238
+ isRichErrorMessages(par) ? par : {
239
+ dev: `Account already linked to a wallet on ${par.toLowerCase()}. There can only be one wallet per network linked to each account.`,
240
+ user: `Your account is already linked to a wallet on ${par.toLowerCase()}`
241
+ }
242
+ );
243
+ this.name = "AccountAlreadyLinkedOnNetworkError";
244
+ }
245
+ };
246
+ var LinkWalletErrors = [
247
+ WalletAlreadyOtherAccountMainWalletError,
248
+ WalletAlreadyLinkedError,
249
+ AccountAlreadyLinkedOnNetworkError
250
+ ];
251
+ var WalletNotLinkedToAccountError = class extends RichError {
252
+ constructor() {
253
+ super(...arguments);
254
+ this.name = "WalletNotLinkedToAccountError";
255
+ this.messages = {
256
+ dev: "Wallet cannot be unlinked because it's not linked to the account currently authenticated",
257
+ user: "The wallet cannot be unlinked because it isn't linked to your account."
258
+ };
259
+ }
260
+ };
261
+ var MainWalletCannotBeUnlinkedError = class extends RichError {
262
+ constructor() {
263
+ super(...arguments);
264
+ this.name = "MainWalletCannotBeUnlinkedError";
265
+ this.messages = {
266
+ dev: "The main wallet of an account cannot be unlinked from the account.",
267
+ user: "This wallet is the main one associated with your account, as such it cannot be unlinked."
268
+ };
269
+ }
270
+ };
271
+ var UnlinkWalletErrors = [WalletNotLinkedToAccountError, MainWalletCannotBeUnlinkedError];
272
+
273
+ // src/errors/core.ts
274
+ var WalletSourceRequestConnectionRejectedError = class extends RichError {
275
+ constructor() {
276
+ super(...arguments);
277
+ this.name = "WalletSourceRequestConnectionRejectedError";
278
+ this.messages = {
279
+ dev: "The connection request was rejected by the user",
280
+ user: "It looks like you've rejected the connection request - if you didn't mean to, please try again"
281
+ };
282
+ }
283
+ };
284
+ var WalletSourceRequestConnectionUnknownError = class extends RichError {
285
+ constructor(par) {
286
+ super(
287
+ isRichErrorMessages(par) ? par : {
288
+ dev: `An unknown error occurred while requesting a connection: ${par}`,
289
+ user: "An unknown error occurred while trying to connect to your wallet - please try again"
290
+ }
291
+ );
292
+ this.name = "WalletSourceRequestConnectionUnknownError";
293
+ }
294
+ };
295
+ var WalletSourceRequestConnectionWalletNotAvailableError = class extends RichError {
296
+ constructor(par) {
297
+ super(
298
+ isRichErrorMessages(par) ? par : {
299
+ dev: `The wallet source for ${par} is not available`,
300
+ user: "An unknown error occurred while trying to connect to your wallet - please try again"
301
+ }
302
+ );
303
+ this.name = "WalletSourceRequestConnectionWalletNotAvailableError";
304
+ }
305
+ };
306
+ var WalletSourceRequestConnectionErrors = [
307
+ WalletSourceRequestConnectionRejectedError,
308
+ WalletSourceRequestConnectionUnknownError,
309
+ WalletSourceRequestConnectionWalletNotAvailableError
310
+ ];
311
+ var NoWalletConnectedForNetworkError = class extends RichError {
312
+ constructor(par) {
313
+ super(
314
+ isRichErrorMessages(par) ? par : {
315
+ dev: `${par} - No wallet is connected to the client`,
316
+ user: "A wallet needs to be connected before performing this action"
317
+ }
318
+ );
319
+ this.name = "NoWalletConnectedForNetworkError";
320
+ }
321
+ };
322
+
323
+ // src/errors/wallet-api.ts
324
+ var WalletAPIRpcUnknownNetworkError = class extends RichError {
325
+ constructor(par) {
326
+ super(
327
+ isRichErrorMessages(par) ? par : {
328
+ dev: `Could not check rpc health for network: ${par}`,
329
+ user: "Rpc health check failed"
330
+ }
331
+ );
332
+ this.name = "WalletApiRpcHealthError";
333
+ }
334
+ };
335
+ var WalletAPIInvalidRequestError = class extends RichError {
336
+ constructor(par) {
337
+ const details = Array.isArray(par) ? `
338
+ Validation errors:
339
+ ${par.map((err) => ` - ${err.path.join(".")}: ${err.message}`).join("\n")}` : "";
340
+ super(
341
+ isRichErrorMessages(par) ? par : {
342
+ dev: `Invalid operation request payload.${details}`,
343
+ user: "Invalid request. Please check your input and try again."
344
+ }
345
+ );
346
+ this.name = "WalletAPIInvalidRequestError";
347
+ }
348
+ };
349
+ var WalletAPIFetchError = class extends RichError {
350
+ constructor(par, statusCode, attempt, maxRetries) {
351
+ super(
352
+ isRichErrorMessages(par) ? par : {
353
+ dev: `Request to ${par} failed (status: ${statusCode}) on attempt ${attempt}/${maxRetries}`,
354
+ user: "Failed to fetch required data. Please try again later."
355
+ }
356
+ );
357
+ this.name = "WalletAPIFetchError";
358
+ }
359
+ };
360
+ var WalletAPIFetchTimeoutError = class extends RichError {
361
+ constructor(par, maxRetries, finalError) {
362
+ const errorMessage = finalError instanceof Error ? finalError.message : String(finalError);
363
+ super(
364
+ isRichErrorMessages(par) ? par : {
365
+ dev: `Request to ${par} failed after ${maxRetries} retries. Final error: ${errorMessage}`,
366
+ user: "Service temporarily unavailable. Please try again later."
367
+ }
368
+ );
369
+ this.name = "WalletAPIFetchTimeoutError";
370
+ }
371
+ };
372
+ var WalletAPIInvalidURLError = class extends RichError {
373
+ constructor(par) {
374
+ super(
375
+ isRichErrorMessages(par) ? par : {
376
+ dev: `Invalid URL provided: ${par}`,
377
+ user: "Invalid request URL configuration."
378
+ }
379
+ );
380
+ this.name = "WalletAPIInvalidURLError";
381
+ }
382
+ };
383
+ var WalletAPIErrors = [
384
+ WalletAPIRpcUnknownNetworkError,
385
+ WalletAPIInvalidRequestError,
386
+ WalletAPIFetchError,
387
+ WalletAPIFetchTimeoutError,
388
+ WalletAPIInvalidURLError
389
+ ];
390
+
391
+ // src/errors/web3auth.ts
392
+ var Web3AuthFrameNotLoading = class extends RichError {
393
+ constructor(par1, error) {
394
+ const _error = error ? typeof error === "string" ? error : error.type : "/";
395
+ super(
396
+ isRichErrorMessages(par1) ? par1 : {
397
+ dev: `Fxhash embedded wallet iframe (at "${par1}") could not be loaded. The following error was retured: ${_error}`,
398
+ user: "Fxhash embedded wallet has not loaded"
399
+ }
400
+ );
401
+ this.name = "Web3AuthFrameNotLoading";
402
+ }
403
+ };
404
+ var Web3AuthFrameNotResponding = class extends RichError {
405
+ constructor(url) {
406
+ super({
407
+ dev: `Fxhash embedded wallet iframe (at "${url}") is not responding to requests`,
408
+ user: "Fxhash embedded wallet is not responding"
409
+ });
410
+ this.name = "Web3AuthFrameNotResponding";
411
+ }
412
+ };
413
+ var Web3AuthFrameNotInitialized = class extends RichError {
414
+ constructor() {
415
+ super(...arguments);
416
+ this.name = "Web3AuthFrameNotInitialized";
417
+ this.messages = {
418
+ dev: "Fxhash embedded wallet <iframe> hasn't been initialized while it should have",
419
+ user: "Fxhash embedded wallet wasn't initialized"
420
+ };
421
+ }
422
+ };
423
+ var Web3AuthFrameAuthenticationError = class extends RichError {
424
+ constructor() {
425
+ super(...arguments);
426
+ this.name = "Web3AuthFrameAuthenticationError";
427
+ this.messages = {
428
+ dev: "An error occurred when attempting to authenticate on Web3Auth",
429
+ user: "Authentication error"
430
+ };
431
+ }
432
+ };
433
+ var Web3AuthFrameFxhashAuthenticationError = class extends RichError {
434
+ constructor() {
435
+ super(...arguments);
436
+ this.name = "Web3AuthFrameFxhashAuthenticationError";
437
+ this.messages = {
438
+ dev: "An error occurred when attempting to authenticate on fxhash",
439
+ user: "Authentication error"
440
+ };
441
+ }
442
+ };
443
+ var Web3AuthFrameLogoutFailedError = class extends RichError {
444
+ constructor() {
445
+ super(...arguments);
446
+ this.name = "Web3AuthFrameLogoutFailedError";
447
+ this.messages = {
448
+ dev: "Logout failed. This is likely an issue on fxhash end.",
449
+ user: "Could not logout your account. Please try again."
450
+ };
451
+ }
452
+ };
453
+ var Web3AuthInitializationFailedError = class extends RichError {
454
+ constructor() {
455
+ super(...arguments);
456
+ this.name = "Web3AuthInitializationFailedError";
457
+ this.messages = {
458
+ dev: "Web3Auth initialization failed, as such the embedded wallet cannot be constructed and used. If this issue persists, please raise it on Github.",
459
+ user: "Could not initialize embedded wallet."
460
+ };
461
+ }
462
+ };
463
+ var Web3AuthFrameUnknownError = class extends RichError {
464
+ constructor() {
465
+ super(...arguments);
466
+ this.name = "Web3AuthFrameUnknownError";
467
+ this.messages = {
468
+ dev: "An unexpected error was raised using Web3Auth",
469
+ user: UnexpectedRichErrorMessages.user
470
+ };
471
+ }
472
+ };
473
+ var Web3AuthFrameErrors = {
474
+ init: [Web3AuthInitializationFailedError],
475
+ getSessionDetails: [Web3AuthFrameAuthenticationError],
476
+ login: [
477
+ Web3AuthFrameAuthenticationError,
478
+ Web3AuthFrameFxhashAuthenticationError,
479
+ Web3AuthFrameUnknownError
480
+ ],
481
+ logout: [Web3AuthFrameLogoutFailedError]
482
+ };
483
+ var AllWeb3AuthFrameErrors = Array().concat.apply(
484
+ [],
485
+ Object.values(Web3AuthFrameErrors)
486
+ );
487
+ export {
488
+ AccountAlreadyLinkedOnNetworkError,
489
+ AllWeb3AuthFrameErrors,
490
+ EmailOTPExpiredError,
491
+ EmailOTPInvalidError,
492
+ EmailOTPLockedError,
493
+ EmailOTPRequestErrors,
494
+ EmailOTPVerificationErrors,
495
+ GraphQLErrors,
496
+ LinkWalletErrors,
497
+ MainWalletCannotBeUnlinkedError,
498
+ NetworkRichError,
499
+ NoWalletConnectedForNetworkError,
500
+ OAuthErrors,
501
+ OAuthMissingInfoError,
502
+ OAuthTokenVerificationError,
503
+ RichError,
504
+ UnexpectedRichError,
505
+ UnexpectedRichErrorMessages,
506
+ UnlinkWalletErrors,
507
+ WalletAPIErrors,
508
+ WalletAPIFetchError,
509
+ WalletAPIFetchTimeoutError,
510
+ WalletAPIInvalidRequestError,
511
+ WalletAPIInvalidURLError,
512
+ WalletAPIRpcUnknownNetworkError,
513
+ WalletAlreadyLinkedError,
514
+ WalletAlreadyOtherAccountMainWalletError,
515
+ WalletNotLinkedToAccountError,
516
+ WalletSourceRequestConnectionErrors,
517
+ WalletSourceRequestConnectionRejectedError,
518
+ WalletSourceRequestConnectionUnknownError,
519
+ WalletSourceRequestConnectionWalletNotAvailableError,
520
+ Web3AuthFrameAuthenticationError,
521
+ Web3AuthFrameErrors,
522
+ Web3AuthFrameFxhashAuthenticationError,
523
+ Web3AuthFrameLogoutFailedError,
524
+ Web3AuthFrameNotInitialized,
525
+ Web3AuthFrameNotLoading,
526
+ Web3AuthFrameNotResponding,
527
+ Web3AuthFrameUnknownError,
528
+ Web3AuthInitializationFailedError,
529
+ isErrorOfKind,
530
+ isFxhashErrorExtensions,
531
+ isRichErrorMessages,
532
+ richError,
533
+ richErrorFromGraphQLError,
534
+ richResultFromGraphQLResponse,
535
+ typedRichErrorFromGraphQLError
536
+ };
537
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors/common.ts","../src/errors/network.ts","../src/errors/graphql/common.ts","../src/errors/graphql/email-otp.ts","../src/utils/rich-error.ts","../src/errors/graphql/oauth.ts","../src/errors/graphql/wallet-linking.ts","../src/errors/core.ts","../src/errors/wallet-api.ts","../src/errors/web3auth.ts"],"sourcesContent":["import type { IEquatableError } from \"@fxhash/utils\"\n\n/**\n * Rich Error Messages are messages with extra data for a better usage of errors\n * passed through the stack. Traditionally, error messages are intended for\n * developers only (and some custom implementation for user error feedback is\n * required for clean UIs).\n */\nexport interface IRichErrorMessages {\n dev?: string\n user?: string\n}\n\n/**\n * Static error messages for \"unexpected\" errors. This payload is reused accross\n * the stack for when error data is missing.\n */\nexport const UnexpectedRichErrorMessages: Readonly<\n Required<IRichErrorMessages>\n> = {\n dev: \"Unexpected error\",\n user: \"Unexpected error\",\n} as const\n\n/**\n * Flatenned Rich Error interface so that Rich Errors can also be passed in a\n * more convenient way. The purpose of this interface is to provide back-\n * compatibility with `Error`, as well as providing an unambiguous `userMessage`\n * property which can be used by front-end applications.\n */\nexport interface IRichError {\n /**\n * Message intended for developers\n */\n message: string\n /**\n * Message intended for users\n */\n userMessage: string\n}\n\n/**\n * An error which provides messages intended for devs & users. This class should\n * be used accross our stack to provide meaningful error objects which can be\n * used for both developers & users. When typed errors are returned, if they all\n * are instances of `RichError`, then client applications can take some simple\n * shortcuts to provide error feedback to both the developer and the user.\n *\n * @example\n *\n * ```ts\n * export class SomeCustomError extends RichError {\n * name = \"SomeCustomError\" as const // `code` will have same value\n * messages = {\n * dev: \"some message for devs\",\n * user: \"some message for users\"\n * }\n * }\n * ```\n *\n * ```ts\n * const res = await something()\n * if (res.isFailure()) {\n * const err = res.error // if this is typed as `RichError`\n * displayErrorOnUI(err.userMessage) // safely display user message\n * }\n * ```\n */\nexport class RichError extends Error implements IRichError, IEquatableError {\n messages: IRichErrorMessages = UnexpectedRichErrorMessages\n messagesOverride: IRichErrorMessages | undefined\n\n constructor(messagesOverride?: IRichErrorMessages) {\n super()\n if (messagesOverride) {\n this.messagesOverride = messagesOverride\n }\n }\n\n private _message(target: \"dev\" | \"user\") {\n return (\n this.messagesOverride?.[target] ||\n this.messages[target] ||\n UnexpectedRichErrorMessages[target]\n )\n }\n\n get message(): string {\n return this._message(\"dev\")\n }\n\n get userMessage(): string {\n return this._message(\"user\")\n }\n\n get code(): string {\n return this.name\n }\n\n public serialize(): IRichErrorSerialized {\n return {\n code: this.code,\n messages: this.messagesOverride || this.messages,\n }\n }\n\n static get code(): string {\n return this.name\n }\n\n /**\n * Instanciates a Rich Error, trying to match the serialized error with the\n * provided Rich Error classes. The `code` property of the serialized payload\n * is matched against `RichError.name`\n *\n * @param serialized A rich error serialized\n * @param expected A list of Rich Error classes which are expected. If the\n * serialized error doesn't match any of these, UnexpectedRichError will be\n * returned.\n *\n * @returns An instance of Rich Error which type matches the `code` property,\n * or {@link UnexpectedRichError} if no match.\n */\n static parse<T extends (typeof RichError)[]>(\n serialized: IRichErrorSerialized,\n expected: T\n ): RichErrorUnion<T> | UnexpectedRichError {\n for (const RichErrorClass of expected) {\n if (RichErrorClass.code === serialized.code) {\n return new RichErrorClass(serialized.messages) as InstanceType<\n T[number]\n >\n }\n }\n return new UnexpectedRichError(serialized.messages)\n }\n\n /**\n * Returns a new instance of {@link UnexpectedRichError}\n * @param messagesOverride Optional overrides of default unexpected messages\n */\n static Unexpected(\n messagesOverride?: IRichErrorMessages\n ): UnexpectedRichError {\n return new UnexpectedRichError(messagesOverride)\n }\n}\n\n/**\n * A Rich error serialized,\n */\nexport interface IRichErrorSerialized {\n code: string\n messages?: IRichErrorMessages\n}\n\n/**\n * A general-purpose error which is thrown when no better error could be\n * inferred from the available context.\n */\nexport class UnexpectedRichError extends RichError {\n name = \"UnexpectedRichError\" as const\n messages: typeof UnexpectedRichErrorMessages = UnexpectedRichErrorMessages\n}\n\n/**\n * Creates an Union of Rich Error instance types from an array of Rich Error\n * classes.\n */\nexport type RichErrorUnion<T extends (typeof RichError)[]> = InstanceType<\n T[number]\n>\n","import { RichError } from \"./common.js\"\n\nexport class NetworkRichError extends RichError {\n name = \"NetworkRichError\" as const\n messages = {\n dev: \"An network error occured.\",\n user: \"Network error\",\n }\n}\n","import {\n IRichErrorSerialized,\n RichError,\n RichErrorUnion,\n UnexpectedRichError,\n} from \"../common.js\"\nimport { NetworkRichError } from \"../network.js\"\n\n/**\n * Format of the GraphQL error extensions returned by the GraphQL API.\n */\nexport interface IFxhashGraphQLErrorExtensions {\n version: \"fxhash@0.1.0\"\n richError: IRichErrorSerialized\n}\n\nexport const GraphQLErrors: (\n | typeof UnexpectedRichError\n | typeof NetworkRichError\n)[] = [NetworkRichError, UnexpectedRichError]\n\nexport type WithGqlErrors<T extends RichError> =\n | T\n | RichErrorUnion<typeof GraphQLErrors>\n","import { RichError, RichErrorUnion } from \"../common.js\"\n\nexport class EmailOTPLockedError extends RichError {\n name = \"EmailOTPLockedError\" as const\n messages = {\n dev: \"Email locked 2h because too many attempts\",\n user: \"This email is locked for verification during 2 hours because of too many attempts in a short period of time\",\n }\n}\n\nexport const EmailOTPRequestErrors: (typeof EmailOTPLockedError)[] = [\n EmailOTPLockedError,\n]\nexport type EmailOTPRequestError = RichErrorUnion<typeof EmailOTPRequestErrors>\n\nexport class EmailOTPInvalidError extends RichError {\n name = \"EmailOTPInvalidError\" as const\n messages = {\n dev: \"Email verification failed because OTP is invalid\",\n user: \"The validation code is invalid\",\n }\n}\n\nexport class EmailOTPExpiredError extends RichError {\n name = \"EmailOTPExpiredError\" as const\n messages = {\n dev: \"Email verification failed because OTP has expired. A new OTP request must be initiated\",\n user: \"Verification code has expired. Please make another request.\",\n }\n}\n\nexport const EmailOTPVerificationErrors: (\n | typeof EmailOTPLockedError\n | typeof EmailOTPInvalidError\n | typeof EmailOTPExpiredError\n)[] = [EmailOTPInvalidError, EmailOTPExpiredError, EmailOTPLockedError]\nexport type EmailOTPVerificationError = RichErrorUnion<\n typeof EmailOTPVerificationErrors\n>\n","import type { CombinedError, OperationResult } from \"@urql/core\"\nimport {\n IFxhashGraphQLErrorExtensions,\n IRichErrorMessages,\n NetworkRichError,\n RichError,\n UnexpectedRichError,\n WithGqlErrors,\n} from \"../index.js\"\nimport { IEquatableError, Result, failure, success } from \"@fxhash/utils\"\n\nexport type TypeOfRichError<T extends RichError> = {\n new (): T\n parse: (typeof RichError)[\"parse\"]\n Unexpected: (typeof RichError)[\"Unexpected\"]\n code: (typeof RichError)[\"code\"]\n}\n\n/**\n * Instanciate a new {@link RichError} using a declarative object. This is\n * useful when Rich Error are instanciated programmatically when type is\n * unknown.\n */\nexport function richError(params: {\n name: string\n messages?: IRichErrorMessages\n}) {\n return Object.assign(new RichError(), params) as RichError\n}\n\nexport function isFxhashErrorExtensions(\n ext: any\n): ext is IFxhashGraphQLErrorExtensions {\n return typeof ext === \"object\" && ext.version === \"fxhash@0.1.0\"\n}\n\n/**\n * Test whether a given value is implementing the {@link IRichErrorMessages}\n * interface.\n * @param value Any value\n */\nexport function isRichErrorMessages(value: any): value is IRichErrorMessages {\n if (typeof value !== \"object\") return false\n return typeof value.dev === \"string\" || typeof value.user === \"string\"\n}\n\n/**\n * Parses the GraphQL error object into a RichError. This function detects\n * fxhash error extensions for outputting user error messages returned by\n * the backend.\n *\n * @param error A GraphQL error response\n *\n * @returns An \"untyped\" rich error constructed by parsing the GraphQL error\n */\nexport function richErrorFromGraphQLError(\n error: CombinedError\n): RichError | UnexpectedRichError | NetworkRichError {\n if (error.graphQLErrors.length > 0) {\n const gqlError = error.graphQLErrors[0]\n if (isFxhashErrorExtensions(gqlError.extensions)) {\n return richError({\n name: gqlError.extensions.richError.code,\n messages: gqlError.extensions.richError.messages,\n })\n }\n return new UnexpectedRichError()\n }\n if (error.networkError) {\n return new NetworkRichError()\n }\n return new UnexpectedRichError()\n}\n\n/**\n * Parses the GraphQL error response to find the fxhash GraphQL error extension,\n * which is used to instanciate a Rich Error from a list of provided RichErrors.\n * The `name` constant property of such classes will be compared to the `code`\n * property of the fxhash error extension to find a match.\n *\n * @param graphQLError GraphQL error response\n * @param expectedErrors An array of RichError classes which will be parsed to\n * find matches between the RichError `name` constant and the `code` returned\n * by the GraphQL fxhash error extension.\n *\n * @returns A RichError instance matchin the error code, or\n * {@link NetworkRichError} if a network error occured, else\n * {@link UnexpectedRichError}\n */\nexport function typedRichErrorFromGraphQLError<T extends (typeof RichError)[]>(\n graphQLError: CombinedError,\n expectedErrors: T\n): WithGqlErrors<InstanceType<T[number]>> {\n if (graphQLError.networkError) {\n return new NetworkRichError()\n }\n if (graphQLError.graphQLErrors.length > 0) {\n const gqlError = graphQLError.graphQLErrors[0]\n if (isFxhashErrorExtensions(gqlError.extensions)) {\n return RichError.parse(gqlError.extensions.richError, expectedErrors)\n }\n return new UnexpectedRichError()\n }\n return new UnexpectedRichError()\n}\n\n/**\n * Returns a `Result<Data, TypedRichError>` by parsing a GraphQL response. If\n * the response has an error, {@link typedRichErrorFromGraphQLError} will be\n * called with such error to return a proper error instance based on the error\n * code in the fxhash graphql error extension (or a a generic error if none).\n *\n * @param operationResult A GraphQL response from fxhash hasura endpoint\n * @param getData A function which takes the response and returns the data (if\n * no data is found it should return `null` | `undefined`, in which case it will\n * fail with UnexpectedError)\n * @param potentialErrors An array of Rich Error classes which could be found in\n * the error response.\n *\n * @example\n *\n * ```ts\n * const emailRequestOTP = async (email) => {\n * return richResultFromGraphQLResponse(\n * await gqlWrapper.client().mutation(Mu_Web3AuthEmailRequestOTP, {\n * email,\n * }),\n * res => res.data?.web3auth_email_request_otp,\n * EmailOTPRequestErrors\n * )\n * },\n * ```\n */\nexport function richResultFromGraphQLResponse<\n T extends (typeof RichError)[],\n Data = any,\n ExtractedData = any,\n>(\n operationResult: OperationResult<Data>,\n getData: (result: OperationResult<Data>) => ExtractedData | undefined | null,\n potentialErrors: T\n): Result<ExtractedData, WithGqlErrors<InstanceType<T[number]>>> {\n const res = operationResult\n if (res.error) {\n return failure(typedRichErrorFromGraphQLError(res.error, potentialErrors))\n }\n const data = getData(res)\n return data\n ? success(data)\n : failure(\n new UnexpectedRichError({\n dev: \"Expected data missing from GraphQL response\",\n })\n )\n}\n\n/**\n * Test if an error is of a certain error kind, among a list of [errors] or\n * [list of errors]. This allows testing multiple array of errors, which are\n * defined quite a lot throughout the errors stack.\n *\n * @param error The error which needs to be tested\n * @param kinds List of [errors]/[array of errors]\n *\n * @returns boolean if error is of given kind\n *\n * @example\n *\n * ```ts\n * isErrorOfKind(\n * someErr,\n * UnexpectedRichError,\n * [SuperError, BigError],\n * SimpleError\n * )\n * ```\n */\nexport function isErrorOfKind<\n Errors extends (IEquatableError | IEquatableError[])[],\n>(\n error: IEquatableError,\n ...kinds: Errors\n): error is Instance<Flatten<Errors>> {\n for (const kind of kinds) {\n if (Array.isArray(kind)) {\n if (isErrorOfKind(error, ...kind)) return true\n } else {\n if (error.name === kind.name) return true\n }\n }\n return false\n}\n\ntype Flatten<T> = T extends (infer U)[] ? Flatten<U> : T\ntype Instance<T> = T extends abstract new (...args: any) => any\n ? InstanceType<T>\n : T\n","import { isRichErrorMessages } from \"../../utils/rich-error.js\"\nimport { IRichErrorMessages, RichError, RichErrorUnion } from \"../common.js\"\nimport { capitalize } from \"@fxhash/utils\"\n\ntype OAuthProvider = \"google\" | \"discord\"\n\nconst couldntSignIn = (provider: OAuthProvider) =>\n `Couldn't sign in using ${capitalize(provider)}`\n\nexport class OAuthTokenVerificationError extends RichError {\n name = \"OAuthTokenVerificationError\" as const\n\n constructor(messages: IRichErrorMessages)\n constructor(provider: OAuthProvider)\n constructor(par: IRichErrorMessages | OAuthProvider) {\n super(\n isRichErrorMessages(par)\n ? par\n : {\n dev: `The provided ${capitalize(par)} OAuth token could not be verified against ${capitalize(par)} services.`,\n user: couldntSignIn(par),\n }\n )\n }\n}\n\nexport class OAuthMissingInfoError extends RichError {\n name = \"OAuthMissingInfoError\" as const\n\n constructor(messages: IRichErrorMessages)\n constructor(provider: OAuthProvider, missing: string[])\n constructor(par: IRichErrorMessages | OAuthProvider, missing?: string[]) {\n super(\n isRichErrorMessages(par)\n ? par\n : {\n dev: `Some user information is missing at the end of the ${capitalize(par)} OAuth authentication flow: ${missing?.join(\", \")}. This shouldn't happen and requires investigation from the fxhash team. Please forward this issue to our team.`,\n user: couldntSignIn(par),\n }\n )\n }\n}\n\nexport const OAuthErrors: (\n | typeof OAuthTokenVerificationError\n | typeof OAuthMissingInfoError\n)[] = [OAuthTokenVerificationError, OAuthMissingInfoError]\nexport type OAuthError = RichErrorUnion<typeof OAuthErrors>\n","import { isRichErrorMessages } from \"../../utils/rich-error.js\"\nimport { IRichErrorMessages, RichError, RichErrorUnion } from \"../common.js\"\n\nexport class WalletAlreadyOtherAccountMainWalletError extends RichError {\n name = \"WalletAlreadyOtherAccountMainWalletError\" as const\n messages = {\n dev: \"Wallet is already the main wallet of another account.\",\n user: \"This wallet is already registered as the main wallet for another account. To link this wallet to your current account, you need to delete the account associated with this wallet first. Please log in to the other account and proceed to delete the account from the profile menu. After this, you can link the wallet to this account.\",\n }\n}\n\nexport class WalletAlreadyLinkedError extends RichError {\n name = \"WalletAlreadyLinkedError\" as const\n messages = {\n dev: \"Wallet is already linked to another account (not as the main wallet)\",\n user: \"This wallet is already associated to another account. You must first connect to your other account and unlink this wallet from it.\",\n }\n}\n\nexport class AccountAlreadyLinkedOnNetworkError extends RichError {\n name = \"AccountAlreadyLinkedOnNetworkError\" as const\n constructor(messages: IRichErrorMessages)\n constructor(network: string)\n constructor(par: IRichErrorMessages | string) {\n super(\n isRichErrorMessages(par)\n ? par\n : {\n dev: `Account already linked to a wallet on ${par.toLowerCase()}. There can only be one wallet per network linked to each account.`,\n user: `Your account is already linked to a wallet on ${par.toLowerCase()}`,\n }\n )\n }\n}\n\nexport const LinkWalletErrors: (\n | typeof WalletAlreadyOtherAccountMainWalletError\n | typeof WalletAlreadyLinkedError\n | typeof AccountAlreadyLinkedOnNetworkError\n)[] = [\n WalletAlreadyOtherAccountMainWalletError,\n WalletAlreadyLinkedError,\n AccountAlreadyLinkedOnNetworkError,\n]\nexport type LinkWalletError = RichErrorUnion<typeof LinkWalletErrors>\n\nexport class WalletNotLinkedToAccountError extends RichError {\n name = \"WalletNotLinkedToAccountError\" as const\n messages = {\n dev: \"Wallet cannot be unlinked because it's not linked to the account currently authenticated\",\n user: \"The wallet cannot be unlinked because it isn't linked to your account.\",\n }\n}\n\nexport class MainWalletCannotBeUnlinkedError extends RichError {\n name = \"MainWalletCannotBeUnlinkedError\" as const\n messages = {\n dev: \"The main wallet of an account cannot be unlinked from the account.\",\n user: \"This wallet is the main one associated with your account, as such it cannot be unlinked.\",\n }\n}\n\nexport const UnlinkWalletErrors: (\n | typeof WalletNotLinkedToAccountError\n | typeof MainWalletCannotBeUnlinkedError\n)[] = [WalletNotLinkedToAccountError, MainWalletCannotBeUnlinkedError]\nexport type UnlinkWalletError = RichErrorUnion<typeof UnlinkWalletErrors>\n","import { isRichErrorMessages } from \"../utils/rich-error.js\"\nimport { IRichErrorMessages, RichError, RichErrorUnion } from \"./common.js\"\n\nexport class WalletSourceRequestConnectionRejectedError extends RichError {\n name = \"WalletSourceRequestConnectionRejectedError\" as const\n messages = {\n dev: \"The connection request was rejected by the user\",\n user: \"It looks like you've rejected the connection request - if you didn't mean to, please try again\",\n }\n}\n\nexport class WalletSourceRequestConnectionUnknownError extends RichError {\n name = \"WalletSourceRequestConnectionUnknownError\" as const\n\n constructor(messages: IRichErrorMessages)\n constructor(network: string)\n constructor(par: IRichErrorMessages | string) {\n super(\n isRichErrorMessages(par)\n ? par\n : {\n dev: `An unknown error occurred while requesting a connection: ${par}`,\n user: \"An unknown error occurred while trying to connect to your wallet - please try again\",\n }\n )\n }\n}\n\nexport class WalletSourceRequestConnectionWalletNotAvailableError extends RichError {\n name = \"WalletSourceRequestConnectionWalletNotAvailableError\" as const\n\n constructor(messages: IRichErrorMessages)\n constructor(network: string)\n constructor(par: IRichErrorMessages | string) {\n super(\n isRichErrorMessages(par)\n ? par\n : {\n dev: `The wallet source for ${par} is not available`,\n user: \"An unknown error occurred while trying to connect to your wallet - please try again\",\n }\n )\n }\n}\n\nexport const WalletSourceRequestConnectionErrors: (\n | typeof WalletSourceRequestConnectionRejectedError\n | typeof WalletSourceRequestConnectionUnknownError\n | typeof WalletSourceRequestConnectionWalletNotAvailableError\n)[] = [\n WalletSourceRequestConnectionRejectedError,\n WalletSourceRequestConnectionUnknownError,\n WalletSourceRequestConnectionWalletNotAvailableError,\n]\nexport type WalletSourceRequestConnectionError = RichErrorUnion<\n typeof WalletSourceRequestConnectionErrors\n>\n\nexport class NoWalletConnectedForNetworkError extends RichError {\n name = \"NoWalletConnectedForNetworkError\" as const\n\n constructor(messages: IRichErrorMessages)\n constructor(network: string)\n constructor(par: IRichErrorMessages | string) {\n super(\n isRichErrorMessages(par)\n ? par\n : {\n dev: `${par} - No wallet is connected to the client`,\n user: \"A wallet needs to be connected before performing this action\",\n }\n )\n }\n}\n","import { isRichErrorMessages } from \"../utils/rich-error.js\"\nimport { RichError, RichErrorUnion, IRichErrorMessages } from \"./common.js\"\n\nexport class WalletAPIRpcUnknownNetworkError extends RichError {\n name = \"WalletApiRpcHealthError\" as const\n\n constructor(messages: IRichErrorMessages)\n constructor(network: string)\n constructor(par: IRichErrorMessages | string) {\n super(\n isRichErrorMessages(par)\n ? par\n : {\n dev: `Could not check rpc health for network: ${par}`,\n user: \"Rpc health check failed\",\n }\n )\n }\n}\n\nexport class WalletAPIInvalidRequestError extends RichError {\n name = \"WalletAPIInvalidRequestError\" as const\n\n constructor(messages: IRichErrorMessages)\n constructor(\n validationErrors?: Array<{ path: (string | number)[]; message: string }>\n )\n constructor(\n par?:\n | IRichErrorMessages\n | Array<{ path: (string | number)[]; message: string }>\n ) {\n const details = Array.isArray(par)\n ? `\\nValidation errors:\\n${par\n .map(err => ` - ${err.path.join(\".\")}: ${err.message}`)\n .join(\"\\n\")}`\n : \"\"\n\n super(\n isRichErrorMessages(par)\n ? par\n : {\n dev: `Invalid operation request payload.${details}`,\n user: \"Invalid request. Please check your input and try again.\",\n }\n )\n }\n}\n\nexport class WalletAPIFetchError extends RichError {\n name = \"WalletAPIFetchError\" as const\n\n constructor(messages: IRichErrorMessages)\n constructor(\n url: string,\n statusCode: number,\n attempt: number,\n maxRetries: number\n )\n constructor(\n par: IRichErrorMessages | string,\n statusCode?: number,\n attempt?: number,\n maxRetries?: number\n ) {\n super(\n isRichErrorMessages(par)\n ? par\n : {\n dev: `Request to ${par} failed (status: ${statusCode}) on attempt ${attempt}/${maxRetries}`,\n user: \"Failed to fetch required data. Please try again later.\",\n }\n )\n }\n}\n\nexport class WalletAPIFetchTimeoutError extends RichError {\n name = \"WalletAPIFetchTimeoutError\" as const\n\n constructor(messages: IRichErrorMessages)\n constructor(url: string, maxRetries: number, finalError: unknown)\n constructor(\n par: IRichErrorMessages | string,\n maxRetries?: number,\n finalError?: unknown\n ) {\n const errorMessage =\n finalError instanceof Error ? finalError.message : String(finalError)\n\n super(\n isRichErrorMessages(par)\n ? par\n : {\n dev: `Request to ${par} failed after ${maxRetries} retries. Final error: ${errorMessage}`,\n user: \"Service temporarily unavailable. Please try again later.\",\n }\n )\n }\n}\n\nexport class WalletAPIInvalidURLError extends RichError {\n name = \"WalletAPIInvalidURLError\" as const\n\n constructor(messages: IRichErrorMessages)\n constructor(url: string)\n constructor(par: IRichErrorMessages | string) {\n super(\n isRichErrorMessages(par)\n ? par\n : {\n dev: `Invalid URL provided: ${par}`,\n user: \"Invalid request URL configuration.\",\n }\n )\n }\n}\n\nexport const WalletAPIErrors: (\n | typeof WalletAPIRpcUnknownNetworkError\n | typeof WalletAPIInvalidRequestError\n | typeof WalletAPIFetchError\n | typeof WalletAPIFetchTimeoutError\n | typeof WalletAPIInvalidURLError\n)[] = [\n WalletAPIRpcUnknownNetworkError,\n WalletAPIInvalidRequestError,\n WalletAPIFetchError,\n WalletAPIFetchTimeoutError,\n WalletAPIInvalidURLError,\n]\nexport type WalletAPIError = RichErrorUnion<typeof WalletAPIErrors>\n","import { TypeOfRichError, isRichErrorMessages } from \"../index.js\"\nimport {\n IRichErrorMessages,\n RichError,\n RichErrorUnion,\n UnexpectedRichErrorMessages,\n} from \"./common.js\"\n\nexport class Web3AuthFrameNotLoading extends RichError {\n name = \"Web3AuthFrameNotLoading\" as const\n\n constructor(url: string, error: string | Event)\n constructor(messages: IRichErrorMessages)\n constructor(par1: string | IRichErrorMessages, error?: string | Event) {\n const _error = error\n ? typeof error === \"string\"\n ? error\n : error.type\n : \"/\"\n super(\n isRichErrorMessages(par1)\n ? par1\n : {\n dev: `Fxhash embedded wallet iframe (at \"${par1}\") could not be loaded. The following error was retured: ${_error}`,\n user: \"Fxhash embedded wallet has not loaded\",\n }\n )\n }\n}\n\nexport class Web3AuthFrameNotResponding extends RichError {\n name = \"Web3AuthFrameNotResponding\" as const\n\n constructor(url: string) {\n super({\n dev: `Fxhash embedded wallet iframe (at \"${url}\") is not responding to requests`,\n user: \"Fxhash embedded wallet is not responding\",\n })\n }\n}\n\nexport class Web3AuthFrameNotInitialized extends RichError {\n name = \"Web3AuthFrameNotInitialized\" as const\n messages = {\n dev: \"Fxhash embedded wallet <iframe> hasn't been initialized while it should have\",\n user: \"Fxhash embedded wallet wasn't initialized\",\n }\n}\n\n// errors related to the initialization of the frame\nexport type Web3AuthFrameInitializationError =\n | Web3AuthFrameNotInitialized\n | Web3AuthFrameNotLoading\n | Web3AuthFrameNotResponding\n\nexport class Web3AuthFrameAuthenticationError extends RichError {\n name = \"Web3AuthFrameAuthenticationError\" as const\n messages = {\n dev: \"An error occurred when attempting to authenticate on Web3Auth\",\n user: \"Authentication error\",\n }\n}\n\nexport class Web3AuthFrameFxhashAuthenticationError extends RichError {\n name = \"Web3AuthFrameFxhashAuthenticationError\" as const\n messages = {\n dev: \"An error occurred when attempting to authenticate on fxhash\",\n user: \"Authentication error\",\n }\n}\n\nexport class Web3AuthFrameLogoutFailedError extends RichError {\n name = \"Web3AuthFrameLogoutFailedError\" as const\n messages = {\n dev: \"Logout failed. This is likely an issue on fxhash end.\",\n user: \"Could not logout your account. Please try again.\",\n }\n}\n\nexport class Web3AuthInitializationFailedError extends RichError {\n name = \"Web3AuthInitializationFailedError\" as const\n messages = {\n dev: \"Web3Auth initialization failed, as such the embedded wallet cannot be constructed and used. If this issue persists, please raise it on Github.\",\n user: \"Could not initialize embedded wallet.\",\n }\n}\n\nexport class Web3AuthFrameUnknownError extends RichError {\n name = \"Web3AuthFrameUnknownError\" as const\n messages: {\n dev: string\n user: string\n } = {\n dev: \"An unexpected error was raised using Web3Auth\",\n user: UnexpectedRichErrorMessages.user,\n }\n}\n\nexport const Web3AuthFrameErrors: {\n init: (typeof Web3AuthInitializationFailedError)[]\n getSessionDetails: (typeof Web3AuthFrameAuthenticationError)[]\n login: (\n | typeof Web3AuthFrameAuthenticationError\n | typeof Web3AuthFrameFxhashAuthenticationError\n | typeof Web3AuthFrameUnknownError\n )[]\n logout: (typeof Web3AuthFrameLogoutFailedError)[]\n} = {\n init: [Web3AuthInitializationFailedError],\n getSessionDetails: [Web3AuthFrameAuthenticationError],\n login: [\n Web3AuthFrameAuthenticationError,\n Web3AuthFrameFxhashAuthenticationError,\n Web3AuthFrameUnknownError,\n ],\n logout: [Web3AuthFrameLogoutFailedError],\n}\n\nexport type Web3AuthFrameError = {\n [K in keyof typeof Web3AuthFrameErrors]: RichErrorUnion<\n (typeof Web3AuthFrameErrors)[K]\n >\n}\n\nexport type AllWeb3AuthFrameError = TypeOfRichError<\n Web3AuthFrameError[keyof Web3AuthFrameError]\n>\nexport const AllWeb3AuthFrameErrors: AllWeb3AuthFrameError[] =\n Array<AllWeb3AuthFrameError>().concat.apply(\n [],\n Object.values(Web3AuthFrameErrors) as any\n )\n"],"mappings":";AAiBO,IAAM,8BAET;AAAA,EACF,KAAK;AAAA,EACL,MAAM;AACR;AA8CO,IAAM,YAAN,cAAwB,MAA6C;AAAA,EAI1E,YAAY,kBAAuC;AACjD,UAAM;AAJR,oBAA+B;AAK7B,QAAI,kBAAkB;AACpB,WAAK,mBAAmB;AAAA,IAC1B;AAAA,EACF;AAAA,EAEQ,SAAS,QAAwB;AACvC,WACE,KAAK,mBAAmB,MAAM,KAC9B,KAAK,SAAS,MAAM,KACpB,4BAA4B,MAAM;AAAA,EAEtC;AAAA,EAEA,IAAI,UAAkB;AACpB,WAAO,KAAK,SAAS,KAAK;AAAA,EAC5B;AAAA,EAEA,IAAI,cAAsB;AACxB,WAAO,KAAK,SAAS,MAAM;AAAA,EAC7B;AAAA,EAEA,IAAI,OAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAAA,EAEO,YAAkC;AACvC,WAAO;AAAA,MACL,MAAM,KAAK;AAAA,MACX,UAAU,KAAK,oBAAoB,KAAK;AAAA,IAC1C;AAAA,EACF;AAAA,EAEA,WAAW,OAAe;AACxB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;A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1D;AAAA;AACL,gBAAO;AACP,oBAAW;AAAA,MACT,KAAK;AAAA,MACL,MAAM;AAAA,IACR;AAAA;AACF;AAEO,IAAM,4BAAN,cAAwC,UAAU;AAAA,EAAlD;AAAA;AACL,gBAAO;AACP,oBAGI;AAAA,MACF,KAAK;AAAA,MACL,MAAM,4BAA4B;AAAA,IACpC;AAAA;AACF;AAEO,IAAM,sBAST;AAAA,EACF,MAAM,CAAC,iCAAiC;AAAA,EACxC,mBAAmB,CAAC,gCAAgC;AAAA,EACpD,OAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAAA,EACA,QAAQ,CAAC,8BAA8B;AACzC;AAWO,IAAM,yBACX,MAA6B,EAAE,OAAO;AAAA,EACpC,CAAC;AAAA,EACD,OAAO,OAAO,mBAAmB;AACnC;","names":[]}
@@ -0,0 +1 @@
1
+ export * from "./rich-error.js";
@@ -0,0 +1,108 @@
1
+ import type { CombinedError, OperationResult } from "@urql/core";
2
+ import { IFxhashGraphQLErrorExtensions, IRichErrorMessages, NetworkRichError, RichError, UnexpectedRichError, WithGqlErrors } from "../index.js";
3
+ import { IEquatableError, Result } from "@fxhash/utils";
4
+ export type TypeOfRichError<T extends RichError> = {
5
+ new (): T
6
+ parse: (typeof RichError)["parse"]
7
+ Unexpected: (typeof RichError)["Unexpected"]
8
+ code: (typeof RichError)["code"]
9
+ };
10
+ /**
11
+ * Instanciate a new {@link RichError} using a declarative object. This is
12
+ * useful when Rich Error are instanciated programmatically when type is
13
+ * unknown.
14
+ */
15
+ export declare function richError(params: {
16
+ name: string
17
+ messages?: IRichErrorMessages
18
+ }): RichError;
19
+ export declare function isFxhashErrorExtensions(ext: any): ext is IFxhashGraphQLErrorExtensions;
20
+ /**
21
+ * Test whether a given value is implementing the {@link IRichErrorMessages}
22
+ * interface.
23
+ * @param value Any value
24
+ */
25
+ export declare function isRichErrorMessages(value: any): value is IRichErrorMessages;
26
+ /**
27
+ * Parses the GraphQL error object into a RichError. This function detects
28
+ * fxhash error extensions for outputting user error messages returned by
29
+ * the backend.
30
+ *
31
+ * @param error A GraphQL error response
32
+ *
33
+ * @returns An "untyped" rich error constructed by parsing the GraphQL error
34
+ */
35
+ export declare function richErrorFromGraphQLError(error: CombinedError): RichError | UnexpectedRichError | NetworkRichError;
36
+ /**
37
+ * Parses the GraphQL error response to find the fxhash GraphQL error extension,
38
+ * which is used to instanciate a Rich Error from a list of provided RichErrors.
39
+ * The `name` constant property of such classes will be compared to the `code`
40
+ * property of the fxhash error extension to find a match.
41
+ *
42
+ * @param graphQLError GraphQL error response
43
+ * @param expectedErrors An array of RichError classes which will be parsed to
44
+ * find matches between the RichError `name` constant and the `code` returned
45
+ * by the GraphQL fxhash error extension.
46
+ *
47
+ * @returns A RichError instance matchin the error code, or
48
+ * {@link NetworkRichError} if a network error occured, else
49
+ * {@link UnexpectedRichError}
50
+ */
51
+ export declare function typedRichErrorFromGraphQLError<T extends (typeof RichError)[]>(graphQLError: CombinedError, expectedErrors: T): WithGqlErrors<InstanceType<T[number]>>;
52
+ /**
53
+ * Returns a `Result<Data, TypedRichError>` by parsing a GraphQL response. If
54
+ * the response has an error, {@link typedRichErrorFromGraphQLError} will be
55
+ * called with such error to return a proper error instance based on the error
56
+ * code in the fxhash graphql error extension (or a a generic error if none).
57
+ *
58
+ * @param operationResult A GraphQL response from fxhash hasura endpoint
59
+ * @param getData A function which takes the response and returns the data (if
60
+ * no data is found it should return `null` | `undefined`, in which case it will
61
+ * fail with UnexpectedError)
62
+ * @param potentialErrors An array of Rich Error classes which could be found in
63
+ * the error response.
64
+ *
65
+ * @example
66
+ *
67
+ * ```ts
68
+ * const emailRequestOTP = async (email) => {
69
+ * return richResultFromGraphQLResponse(
70
+ * await gqlWrapper.client().mutation(Mu_Web3AuthEmailRequestOTP, {
71
+ * email,
72
+ * }),
73
+ * res => res.data?.web3auth_email_request_otp,
74
+ * EmailOTPRequestErrors
75
+ * )
76
+ * },
77
+ * ```
78
+ */
79
+ export declare function richResultFromGraphQLResponse<
80
+ T extends (typeof RichError)[],
81
+ Data = any,
82
+ ExtractedData = any
83
+ >(operationResult: OperationResult<Data>, getData: (result: OperationResult<Data>) => ExtractedData | undefined | null, potentialErrors: T): Result<ExtractedData, WithGqlErrors<InstanceType<T[number]>>>;
84
+ /**
85
+ * Test if an error is of a certain error kind, among a list of [errors] or
86
+ * [list of errors]. This allows testing multiple array of errors, which are
87
+ * defined quite a lot throughout the errors stack.
88
+ *
89
+ * @param error The error which needs to be tested
90
+ * @param kinds List of [errors]/[array of errors]
91
+ *
92
+ * @returns boolean if error is of given kind
93
+ *
94
+ * @example
95
+ *
96
+ * ```ts
97
+ * isErrorOfKind(
98
+ * someErr,
99
+ * UnexpectedRichError,
100
+ * [SuperError, BigError],
101
+ * SimpleError
102
+ * )
103
+ * ```
104
+ */
105
+ export declare function isErrorOfKind<Errors extends (IEquatableError | IEquatableError[])[]>(error: IEquatableError, ...kinds: Errors): error is Instance<Flatten<Errors>>;
106
+ type Flatten<T> = T extends (infer U)[] ? Flatten<U> : T;
107
+ type Instance<T> = T extends abstract new (...args: any) => any ? InstanceType<T> : T;
108
+ export {};
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@fxhash/errors",
3
+ "description": "Application-wide errors which are exposed and used by different parts of our stack.",
4
+ "version": "0.0.10",
5
+ "author": "fxhash",
6
+ "dependencies": {
7
+ "@urql/core": "4.1.4",
8
+ "@fxhash/utils": "0.0.3"
9
+ },
10
+ "devDependencies": {
11
+ "@types/node": "18.7.13",
12
+ "tslib": "2.6.2",
13
+ "tsup": "8.4.0",
14
+ "typescript": "5.8.2",
15
+ "unplugin-isolated-decl": "0.13.6",
16
+ "@fxhash/tsconfig": "0.0.1"
17
+ },
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "default": "./dist/index.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "license": "MIT",
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "repository": "fxhash/fxhash-package",
32
+ "type": "module",
33
+ "scripts": {
34
+ "build": "tsup && tsc --noEmit",
35
+ "dev": "tsup --watch"
36
+ }
37
+ }