forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
394 lines (366 loc) · 11.5 KB
/
Copy patherrors.ts
File metadata and controls
394 lines (366 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/**
* Error Handling Utilities
*
* Provides user-friendly error messages and error classification
* for the Mova Store application.
*/
// =============================================================================
// Error Types
// =============================================================================
export type ErrorSeverity = "error" | "warning" | "info";
export interface AppError {
code: string;
message: string;
userMessage: string;
severity: ErrorSeverity;
recoverable: boolean;
action?: string;
}
// =============================================================================
// Stellar/Soroban Error Messages
// =============================================================================
const STELLAR_ERRORS: Record<string, AppError> = {
// Contract errors
NotInitialized: {
code: "STELLAR_NOT_INITIALIZED",
message: "Contract not initialized",
userMessage: "The payment system is not configured. Please contact support.",
severity: "error",
recoverable: false,
},
AlreadyInitialized: {
code: "STELLAR_ALREADY_INITIALIZED",
message: "Contract already initialized",
userMessage: "The payment system is already set up.",
severity: "warning",
recoverable: false,
},
InvalidAmount: {
code: "STELLAR_INVALID_AMOUNT",
message: "Invalid payment amount",
userMessage: "The payment amount is invalid. Please check your cart and try again.",
severity: "error",
recoverable: true,
action: "Check cart total",
},
OrderAlreadyPaid: {
code: "STELLAR_ORDER_ALREADY_PAID",
message: "Order already paid",
userMessage: "This order has already been paid. If you believe this is an error, please contact support.",
severity: "warning",
recoverable: false,
},
OrderNotFound: {
code: "STELLAR_ORDER_NOT_FOUND",
message: "Order not found",
userMessage: "We couldn't find this order. It may have expired or been processed.",
severity: "error",
recoverable: false,
},
TokenNotAllowed: {
code: "STELLAR_TOKEN_NOT_ALLOWED",
message: "Token not allowed",
userMessage: "This payment token is not accepted. Please try a different payment method.",
severity: "error",
recoverable: true,
action: "Try different token",
},
InvalidOrderStatus: {
code: "STELLAR_INVALID_ORDER_STATUS",
message: "Invalid order status",
userMessage: "This order cannot be processed in its current state.",
severity: "error",
recoverable: false,
},
// Wallet errors
WalletNotConnected: {
code: "WALLET_NOT_CONNECTED",
message: "Wallet not connected",
userMessage: "Please connect your Freighter wallet to continue.",
severity: "warning",
recoverable: true,
action: "Connect wallet",
},
WalletNotInstalled: {
code: "WALLET_NOT_INSTALLED",
message: "Freighter not installed",
userMessage: "Please install the Freighter wallet extension to pay with Stellar.",
severity: "warning",
recoverable: true,
action: "Install Freighter",
},
WrongNetwork: {
code: "WALLET_WRONG_NETWORK",
message: "Wrong network",
userMessage: "Please switch your Freighter wallet to the correct network.",
severity: "warning",
recoverable: true,
action: "Switch network",
},
UserRejected: {
code: "WALLET_USER_REJECTED",
message: "Transaction rejected by user",
userMessage: "You cancelled the transaction. Click 'Pay' again when you're ready.",
severity: "info",
recoverable: true,
},
// Account errors
AccountNotFunded: {
code: "ACCOUNT_NOT_FUNDED",
message: "Account not funded",
userMessage: "Your Stellar account needs to be funded before making payments.",
severity: "warning",
recoverable: true,
action: "Fund account",
},
InsufficientBalance: {
code: "ACCOUNT_INSUFFICIENT_BALANCE",
message: "Insufficient balance",
userMessage: "You don't have enough funds to complete this payment. Please add more to your wallet.",
severity: "error",
recoverable: true,
action: "Add funds",
},
NoTrustline: {
code: "ACCOUNT_NO_TRUSTLINE",
message: "No trustline for token",
userMessage: "Your wallet needs to trust USDC before receiving payments. We'll set this up for you.",
severity: "info",
recoverable: true,
},
// Transaction errors
TransactionFailed: {
code: "TX_FAILED",
message: "Transaction failed",
userMessage: "The payment couldn't be processed. Please try again.",
severity: "error",
recoverable: true,
action: "Try again",
},
TransactionTimeout: {
code: "TX_TIMEOUT",
message: "Transaction timed out",
userMessage: "The transaction is taking longer than expected. Please check your wallet for the status.",
severity: "warning",
recoverable: true,
action: "Check wallet",
},
SimulationFailed: {
code: "TX_SIMULATION_FAILED",
message: "Transaction simulation failed",
userMessage: "We couldn't verify this transaction. Please check your balance and try again.",
severity: "error",
recoverable: true,
},
// Network errors
NetworkError: {
code: "NETWORK_ERROR",
message: "Network error",
userMessage: "We're having trouble connecting to the Stellar network. Please check your internet connection.",
severity: "error",
recoverable: true,
action: "Retry",
},
RpcError: {
code: "RPC_ERROR",
message: "RPC server error",
userMessage: "The payment server is temporarily unavailable. Please try again in a moment.",
severity: "error",
recoverable: true,
action: "Retry",
},
};
// =============================================================================
// Auth Error Messages (Supabase)
// =============================================================================
const AUTH_ERRORS: Record<string, AppError> = {
"User already registered": {
code: "AUTH_EMAIL_EXISTS",
message: "Email already in use",
userMessage: "This email is already registered. Try logging in instead.",
severity: "warning",
recoverable: true,
action: "Login",
},
"Invalid login credentials": {
code: "AUTH_INVALID_CREDENTIALS",
message: "Invalid credentials",
userMessage: "Incorrect email or password. Please try again.",
severity: "error",
recoverable: true,
},
"Email not confirmed": {
code: "AUTH_EMAIL_NOT_CONFIRMED",
message: "Email not confirmed",
userMessage: "Please confirm your email before signing in.",
severity: "warning",
recoverable: true,
},
"Password should be at least 6 characters": {
code: "AUTH_WEAK_PASSWORD",
message: "Weak password",
userMessage: "Please choose a stronger password (at least 6 characters).",
severity: "warning",
recoverable: true,
},
"Unable to validate email address: invalid format": {
code: "AUTH_INVALID_EMAIL",
message: "Invalid email",
userMessage: "Please enter a valid email address.",
severity: "error",
recoverable: true,
},
};
// =============================================================================
// General Error Messages
// =============================================================================
const GENERAL_ERRORS: Record<string, AppError> = {
ValidationError: {
code: "VALIDATION_ERROR",
message: "Validation error",
userMessage: "Please check your input and try again.",
severity: "warning",
recoverable: true,
},
NotFound: {
code: "NOT_FOUND",
message: "Resource not found",
userMessage: "We couldn't find what you're looking for.",
severity: "error",
recoverable: false,
},
Unauthorized: {
code: "UNAUTHORIZED",
message: "Unauthorized",
userMessage: "Please log in to continue.",
severity: "warning",
recoverable: true,
action: "Login",
},
Forbidden: {
code: "FORBIDDEN",
message: "Access denied",
userMessage: "You don't have permission to access this.",
severity: "error",
recoverable: false,
},
ServerError: {
code: "SERVER_ERROR",
message: "Server error",
userMessage: "Something went wrong on our end. Please try again later.",
severity: "error",
recoverable: true,
action: "Retry",
},
Unknown: {
code: "UNKNOWN_ERROR",
message: "Unknown error",
userMessage: "Something unexpected happened. Please try again.",
severity: "error",
recoverable: true,
action: "Retry",
},
};
// =============================================================================
// Error Parsing
// =============================================================================
/**
* Parses an error and returns a user-friendly AppError object.
*/
export function parseError(error: unknown): AppError {
// Handle null/undefined
if (!error) {
return GENERAL_ERRORS.Unknown;
}
// Handle string errors
if (typeof error === "string") {
return parseErrorMessage(error);
}
// Handle Error objects
if (error instanceof Error) {
// Check for auth errors
const authCode = (error as { code?: string }).code;
if (authCode && AUTH_ERRORS[authCode]) {
return AUTH_ERRORS[authCode];
}
return parseErrorMessage(error.message);
}
// Handle objects with message property
if (typeof error === "object" && "message" in error) {
return parseErrorMessage(String((error as { message: unknown }).message));
}
return GENERAL_ERRORS.Unknown;
}
/**
* Parses an error message string and matches it to known errors.
*/
function parseErrorMessage(message: string): AppError {
const lowerMessage = message.toLowerCase();
// Check Stellar errors
for (const [key, appError] of Object.entries(STELLAR_ERRORS)) {
if (
lowerMessage.includes(key.toLowerCase()) ||
lowerMessage.includes(appError.code.toLowerCase())
) {
return appError;
}
}
// Check for common error patterns
if (lowerMessage.includes("insufficient") || lowerMessage.includes("balance")) {
return STELLAR_ERRORS.InsufficientBalance;
}
if (lowerMessage.includes("rejected") || lowerMessage.includes("cancelled")) {
return STELLAR_ERRORS.UserRejected;
}
if (lowerMessage.includes("timeout") || lowerMessage.includes("timed out")) {
return STELLAR_ERRORS.TransactionTimeout;
}
if (lowerMessage.includes("network") || lowerMessage.includes("connection")) {
return STELLAR_ERRORS.NetworkError;
}
if (lowerMessage.includes("freighter") && lowerMessage.includes("install")) {
return STELLAR_ERRORS.WalletNotInstalled;
}
// Check auth errors by message
for (const [key, appError] of Object.entries(AUTH_ERRORS)) {
if (lowerMessage.includes(key.toLowerCase()) || lowerMessage.includes(appError.code.toLowerCase())) {
return appError;
}
}
// Return generic error with original message
return {
...GENERAL_ERRORS.Unknown,
message,
userMessage: message.length > 100 ? "An error occurred. Please try again." : message,
};
}
/**
* Creates a custom AppError with user-friendly message.
*/
export function createError(
code: string,
message: string,
userMessage: string,
options: Partial<AppError> = {}
): AppError {
return {
code,
message,
userMessage,
severity: options.severity ?? "error",
recoverable: options.recoverable ?? true,
action: options.action,
};
}
/**
* Gets a user-friendly message from any error.
*/
export function getUserMessage(error: unknown): string {
return parseError(error).userMessage;
}
/**
* Checks if an error is recoverable (user can retry).
*/
export function isRecoverable(error: unknown): boolean {
return parseError(error).recoverable;
}