|
1 |
| -import type { ClerkAPIError, ClerkAPIErrorJSON } from '@clerk/types'; |
2 |
| - |
3 |
| -interface ClerkAPIResponseOptions { |
4 |
| - data: ClerkAPIErrorJSON[]; |
5 |
| - status: number; |
6 |
| -} |
7 |
| - |
8 |
| -// For a comprehensive Metamask error list, please see |
9 |
| -// https://docs.metamask.io/guide/ethereum-provider.html#errors |
10 |
| -export interface MetamaskError extends Error { |
11 |
| - code: 4001 | 32602 | 32603; |
12 |
| - message: string; |
13 |
| - data?: unknown; |
14 |
| -} |
15 |
| - |
16 |
| -export function isKnownError(error: any) { |
17 |
| - return isClerkAPIResponseError(error) || isMetamaskError(error); |
18 |
| -} |
19 |
| - |
20 |
| -export function isClerkAPIResponseError(err: any): err is ClerkAPIResponseError { |
21 |
| - return 'clerkError' in err; |
22 |
| -} |
23 |
| - |
24 |
| -export function isMetamaskError(err: any): err is MetamaskError { |
25 |
| - return 'code' in err && [4001, 32602, 32603].includes(err.code) && 'message' in err; |
26 |
| -} |
27 |
| - |
28 |
| -export function parseErrors(data: ClerkAPIErrorJSON[] = []): ClerkAPIError[] { |
29 |
| - return data.length > 0 ? data.map(parseError) : []; |
30 |
| -} |
31 |
| - |
32 |
| -export function parseError(error: ClerkAPIErrorJSON): ClerkAPIError { |
33 |
| - return { |
34 |
| - code: error.code, |
35 |
| - message: error.message, |
36 |
| - longMessage: error.long_message, |
37 |
| - meta: { |
38 |
| - paramName: error?.meta?.param_name, |
39 |
| - sessionId: error?.meta?.session_id, |
40 |
| - emailAddresses: error?.meta?.email_addresses, |
41 |
| - }, |
42 |
| - }; |
43 |
| -} |
44 |
| - |
45 |
| -export class ClerkAPIResponseError extends Error { |
46 |
| - clerkError: true; |
47 |
| - |
48 |
| - status: number; |
49 |
| - message: string; |
50 |
| - |
51 |
| - errors: ClerkAPIError[]; |
52 |
| - |
53 |
| - constructor(message: string, { data, status }: ClerkAPIResponseOptions) { |
54 |
| - super(message); |
55 |
| - |
56 |
| - Object.setPrototypeOf(this, ClerkAPIResponseError.prototype); |
57 |
| - |
58 |
| - this.status = status; |
59 |
| - this.message = message; |
60 |
| - this.clerkError = true; |
61 |
| - this.errors = parseErrors(data); |
62 |
| - } |
63 |
| - |
64 |
| - public toString = () => { |
65 |
| - return `[${this.name}]\nMessage:${this.message}\nStatus:${this.status}\nSerialized errors: ${this.errors.map(e => |
66 |
| - JSON.stringify(e), |
67 |
| - )}`; |
68 |
| - }; |
69 |
| -} |
70 |
| - |
71 |
| -export class MagicLinkError extends Error { |
72 |
| - code: string; |
73 |
| - |
74 |
| - constructor(code: string) { |
75 |
| - super(code); |
76 |
| - this.code = code; |
77 |
| - Object.setPrototypeOf(this, MagicLinkError.prototype); |
78 |
| - } |
79 |
| -} |
80 |
| -// Check if the error is a MagicLinkError. |
81 |
| - |
82 |
| -export function isMagicLinkError(err: Error): err is MagicLinkError { |
83 |
| - return err instanceof MagicLinkError; |
84 |
| -} |
85 |
| - |
86 |
| -export const MagicLinkErrorCode = { |
87 |
| - Expired: 'expired', |
88 |
| - Failed: 'failed', |
89 |
| -}; |
| 1 | +export { |
| 2 | + isKnownError, |
| 3 | + isMagicLinkError, |
| 4 | + isMetamaskError, |
| 5 | + isClerkAPIResponseError, |
| 6 | + MagicLinkErrorCode, |
| 7 | + parseError, |
| 8 | + parseErrors, |
| 9 | + MagicLinkError, |
| 10 | + ClerkAPIResponseError, |
| 11 | +} from '@clerk/shared'; |
| 12 | +export type { MetamaskError } from '@clerk/shared'; |
0 commit comments