-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathtranslatePerpsError.ts
More file actions
417 lines (386 loc) · 14.6 KB
/
Copy pathtranslatePerpsError.ts
File metadata and controls
417 lines (386 loc) · 14.6 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import { strings } from '../../../../../locales/i18n';
import {
PERPS_ERROR_CODES,
type PerpsErrorCode,
type PerpsDebugLogger,
} from '@metamask/perps-controller';
/**
* Optional debug logger for error handling functions.
* When provided, enables detailed logging for debugging.
*/
export type ErrorHandlerDebugLogger = PerpsDebugLogger | undefined;
/**
* Maps error codes to i18n keys
*/
export const ERROR_CODE_TO_I18N_KEY: Record<PerpsErrorCode, string> = {
[PERPS_ERROR_CODES.CLIENT_NOT_INITIALIZED]:
'perps.errors.clientNotInitialized',
[PERPS_ERROR_CODES.CLIENT_REINITIALIZING]:
'perps.errors.clientReinitializing',
[PERPS_ERROR_CODES.PROVIDER_NOT_AVAILABLE]:
'perps.errors.providerNotAvailable',
[PERPS_ERROR_CODES.TOKEN_NOT_SUPPORTED]: 'perps.errors.tokenNotSupported',
[PERPS_ERROR_CODES.BRIDGE_CONTRACT_NOT_FOUND]:
'perps.errors.bridgeContractNotFound',
[PERPS_ERROR_CODES.WITHDRAW_FAILED]: 'perps.errors.withdrawFailed',
[PERPS_ERROR_CODES.POSITIONS_FAILED]: 'perps.errors.positionsFailed',
[PERPS_ERROR_CODES.ACCOUNT_STATE_FAILED]: 'perps.errors.accountStateFailed',
[PERPS_ERROR_CODES.MARKETS_FAILED]: 'perps.errors.marketsFailed',
[PERPS_ERROR_CODES.UNKNOWN_ERROR]: 'perps.errors.unknownError',
[PERPS_ERROR_CODES.ORDER_LEVERAGE_REDUCTION_FAILED]:
'perps.errors.orderLeverageReductionFailed',
[PERPS_ERROR_CODES.IOC_CANCEL]: 'perps.errors.insufficientLiquidity',
[PERPS_ERROR_CODES.CONNECTION_TIMEOUT]: 'perps.errors.connectionTimeout',
// Withdraw validation errors
[PERPS_ERROR_CODES.WITHDRAW_ASSET_ID_REQUIRED]:
'perps.errors.withdrawValidation.assetIdRequired',
[PERPS_ERROR_CODES.WITHDRAW_AMOUNT_REQUIRED]:
'perps.errors.withdrawValidation.amountRequired',
[PERPS_ERROR_CODES.WITHDRAW_AMOUNT_POSITIVE]:
'perps.errors.withdrawValidation.amountPositive',
[PERPS_ERROR_CODES.WITHDRAW_INVALID_DESTINATION]:
'perps.errors.withdrawValidation.invalidDestination',
[PERPS_ERROR_CODES.WITHDRAW_ASSET_NOT_SUPPORTED]:
'perps.errors.withdrawValidation.assetNotSupported',
[PERPS_ERROR_CODES.WITHDRAW_INSUFFICIENT_BALANCE]:
'perps.errors.withdrawValidation.insufficientBalance',
// Deposit validation errors
[PERPS_ERROR_CODES.DEPOSIT_ASSET_ID_REQUIRED]:
'perps.errors.depositValidation.assetIdRequired',
[PERPS_ERROR_CODES.DEPOSIT_AMOUNT_REQUIRED]:
'perps.errors.depositValidation.amountRequired',
[PERPS_ERROR_CODES.DEPOSIT_AMOUNT_POSITIVE]:
'perps.errors.depositValidation.amountPositive',
[PERPS_ERROR_CODES.DEPOSIT_MINIMUM_AMOUNT]: 'perps.errors.minimumDeposit',
// Order validation errors
[PERPS_ERROR_CODES.ORDER_COIN_REQUIRED]:
'perps.errors.orderValidation.coinRequired',
[PERPS_ERROR_CODES.ORDER_LIMIT_PRICE_REQUIRED]:
'perps.errors.orderValidation.limitPriceRequired',
[PERPS_ERROR_CODES.ORDER_PRICE_POSITIVE]:
'perps.errors.orderValidation.pricePositive',
[PERPS_ERROR_CODES.ORDER_UNKNOWN_COIN]:
'perps.errors.orderValidation.unknownCoin',
[PERPS_ERROR_CODES.ORDER_SIZE_POSITIVE]:
'perps.errors.orderValidation.sizePositive',
[PERPS_ERROR_CODES.ORDER_PRICE_REQUIRED]:
'perps.order.validation.price_required',
[PERPS_ERROR_CODES.ORDER_SIZE_MIN]: 'perps.order.validation.minimum_amount',
[PERPS_ERROR_CODES.ORDER_LEVERAGE_INVALID]:
'perps.order.validation.invalid_leverage',
[PERPS_ERROR_CODES.ORDER_LEVERAGE_BELOW_POSITION]:
'perps.order.validation.leverage_below_position',
[PERPS_ERROR_CODES.ORDER_MAX_VALUE_EXCEEDED]:
'perps.order.validation.max_order_value',
// HyperLiquid client/service errors
[PERPS_ERROR_CODES.EXCHANGE_CLIENT_NOT_AVAILABLE]:
'perps.errors.exchangeClientNotAvailable',
[PERPS_ERROR_CODES.INFO_CLIENT_NOT_AVAILABLE]:
'perps.errors.infoClientNotAvailable',
[PERPS_ERROR_CODES.SUBSCRIPTION_CLIENT_NOT_AVAILABLE]:
'perps.errors.subscriptionClientNotAvailable',
// Wallet/account errors
[PERPS_ERROR_CODES.NO_ACCOUNT_SELECTED]: 'perps.errors.noAccountSelected',
// Keyring locked errors are handled silently in ensure* methods; never shown to users
[PERPS_ERROR_CODES.KEYRING_LOCKED]: 'perps.errors.unknownError',
[PERPS_ERROR_CODES.INVALID_ADDRESS_FORMAT]:
'perps.errors.invalidAddressFormat',
// Transfer/swap errors
[PERPS_ERROR_CODES.TRANSFER_FAILED]: 'perps.errors.transferFailed',
[PERPS_ERROR_CODES.SWAP_FAILED]: 'perps.errors.swapFailed',
[PERPS_ERROR_CODES.SPOT_PAIR_NOT_FOUND]: 'perps.errors.spotPairNotFound',
[PERPS_ERROR_CODES.PRICE_UNAVAILABLE]: 'perps.errors.priceUnavailable',
// Market/collateral errors
[PERPS_ERROR_CODES.UNSUPPORTED_COLLATERAL]:
'perps.errors.unsupportedCollateral',
// Batch operation errors
[PERPS_ERROR_CODES.BATCH_CANCEL_FAILED]: 'perps.errors.batchCancelFailed',
[PERPS_ERROR_CODES.BATCH_CLOSE_FAILED]: 'perps.errors.batchCloseFailed',
// Position/margin errors
[PERPS_ERROR_CODES.INSUFFICIENT_MARGIN]: 'perps.errors.insufficientMargin',
[PERPS_ERROR_CODES.INSUFFICIENT_BALANCE]: 'perps.errors.insufficientBalance',
[PERPS_ERROR_CODES.REDUCE_ONLY_VIOLATION]: 'perps.errors.reduceOnlyViolation',
[PERPS_ERROR_CODES.POSITION_WOULD_FLIP]: 'perps.errors.positionWouldFlip',
[PERPS_ERROR_CODES.MARGIN_ADJUSTMENT_FAILED]:
'perps.errors.marginAdjustmentFailed',
[PERPS_ERROR_CODES.TPSL_UPDATE_FAILED]: 'perps.errors.tpslUpdateFailed',
// Order execution errors
[PERPS_ERROR_CODES.ORDER_REJECTED]: 'perps.errors.orderRejected',
[PERPS_ERROR_CODES.SLIPPAGE_EXCEEDED]: 'perps.errors.slippageExceeded',
[PERPS_ERROR_CODES.RATE_LIMIT_EXCEEDED]: 'perps.errors.rateLimitExceeded',
// Network/service errors
[PERPS_ERROR_CODES.SERVICE_UNAVAILABLE]: 'perps.errors.serviceUnavailable',
[PERPS_ERROR_CODES.NETWORK_ERROR]: 'perps.errors.networkErrorSimple',
};
/**
* Pattern matching for common HyperLiquid API error messages.
* Maps regex patterns to error codes for user-friendly translation.
*
* IMPORTANT: Order matters - more specific patterns MUST come before general ones.
* For example, "Transfer failed: network error" should match NETWORK_ERROR, not TRANSFER_FAILED.
*/
const API_ERROR_PATTERNS: {
pattern: RegExp;
errorCode: PerpsErrorCode;
}[] = [
// === SPECIFIC PATTERNS FIRST ===
// Network/service errors - check these early since they can appear in compound errors
// e.g., "Transfer failed: network error" should match NETWORK_ERROR
{
pattern: /timeout|timed out/i,
errorCode: PERPS_ERROR_CODES.CONNECTION_TIMEOUT,
},
{
pattern: /service unavailable|503|temporarily unavailable/i,
errorCode: PERPS_ERROR_CODES.SERVICE_UNAVAILABLE,
},
{
pattern: /network error|fetch failed|connection failed/i,
errorCode: PERPS_ERROR_CODES.NETWORK_ERROR,
},
// Rate limiting
{
pattern: /rate limit|too many requests|throttl/i,
errorCode: PERPS_ERROR_CODES.RATE_LIMIT_EXCEEDED,
},
// Margin/balance errors
{
pattern: /margin available|not enough margin|insufficient margin/i,
errorCode: PERPS_ERROR_CODES.INSUFFICIENT_MARGIN,
},
{
pattern: /insufficient balance|not enough balance/i,
errorCode: PERPS_ERROR_CODES.INSUFFICIENT_BALANCE,
},
// Order execution errors
{
pattern: /reduce only|reduceOnly/i,
errorCode: PERPS_ERROR_CODES.REDUCE_ONLY_VIOLATION,
},
{
pattern: /position would flip|would flip position/i,
errorCode: PERPS_ERROR_CODES.POSITION_WOULD_FLIP,
},
{
pattern: /slippage|price moved too much/i,
errorCode: PERPS_ERROR_CODES.SLIPPAGE_EXCEEDED,
},
{
pattern: /insufficient liquidity|no liquidity|IOC.*cancel/i,
errorCode: PERPS_ERROR_CODES.IOC_CANCEL,
},
{
pattern: /order rejected|rejected order/i,
errorCode: PERPS_ERROR_CODES.ORDER_REJECTED,
},
// Data errors
{
pattern:
/spot pair not found|trading pair not found|USDH.*USDC.*not found/i,
errorCode: PERPS_ERROR_CODES.SPOT_PAIR_NOT_FOUND,
},
{
pattern: /no price available|price unavailable|price data unavailable/i,
errorCode: PERPS_ERROR_CODES.PRICE_UNAVAILABLE,
},
// Batch operation errors - use specific patterns to avoid matching single-order errors
// e.g., "Order cancellation failed" should NOT match batch cancel
{
pattern: /batch cancel|cancel all|bulk cancel|multiple.*cancel/i,
errorCode: PERPS_ERROR_CODES.BATCH_CANCEL_FAILED,
},
{
pattern: /batch close|close all|bulk close|multiple.*close/i,
errorCode: PERPS_ERROR_CODES.BATCH_CLOSE_FAILED,
},
// Leverage errors
{
pattern: /cannot reduce.*leverage|leverage reduction/i,
errorCode: PERPS_ERROR_CODES.ORDER_LEVERAGE_REDUCTION_FAILED,
},
// === GENERIC PATTERNS LAST ===
// These are catch-all patterns that should only match if no specific pattern matched
{
pattern: /transfer failed/i,
errorCode: PERPS_ERROR_CODES.TRANSFER_FAILED,
},
{
pattern: /swap failed/i,
errorCode: PERPS_ERROR_CODES.SWAP_FAILED,
},
];
/**
* Attempts to match an error string against known API error patterns.
* @param errorString - The error message to match
* @returns The matched error code or null if no match found
*/
function matchApiErrorPattern(errorString: string): PerpsErrorCode | null {
for (const { pattern, errorCode } of API_ERROR_PATTERNS) {
if (pattern.test(errorString)) {
return errorCode;
}
}
return null;
}
/**
* Translates an error code to a localized message
* @param error - Error code string, Error object, or any other value
* @param data - Optional data for interpolation in error messages
* @returns Localized error message
*/
export function translatePerpsError(
error: unknown,
data?: Record<string, unknown>,
): string {
// Handle null or undefined
if (error === null || error === undefined) {
return strings('perps.errors.unknownError');
}
// Handle error code strings
if (typeof error === 'string' && error in ERROR_CODE_TO_I18N_KEY) {
const i18nKey = ERROR_CODE_TO_I18N_KEY[error as PerpsErrorCode];
return strings(i18nKey, data || {});
}
// Handle standard Error objects
if (error instanceof Error) {
// Check if error message is an error code
if (error.message in ERROR_CODE_TO_I18N_KEY) {
const i18nKey = ERROR_CODE_TO_I18N_KEY[error.message as PerpsErrorCode];
return strings(i18nKey, data || {});
}
// Try pattern matching for API error messages
const matchedErrorCode = matchApiErrorPattern(error.message);
if (matchedErrorCode) {
const i18nKey = ERROR_CODE_TO_I18N_KEY[matchedErrorCode];
return strings(i18nKey, data || {});
}
return error.message;
}
// Handle string errors that might be error codes
if (typeof error === 'string') {
// Try pattern matching for API error messages
const matchedErrorCode = matchApiErrorPattern(error);
if (matchedErrorCode) {
const i18nKey = ERROR_CODE_TO_I18N_KEY[matchedErrorCode];
return strings(i18nKey, data || {});
}
return error;
}
// Handle objects, numbers, and other types
return strings('perps.errors.unknownError');
}
/**
* Checks if an error is a specific PerpsError code
*/
export function isPerpsErrorCode(
error: unknown,
code: PerpsErrorCode,
): boolean {
if (error instanceof Error && error.message === code) {
return true;
}
return error === code;
}
/**
* Parameters for handling Perps errors
*/
export interface HandlePerpsErrorParams {
error: unknown;
context?: {
providerId?: string;
token?: string;
amount?: string;
symbol?: string;
address?: string;
available?: string;
requested?: string;
assetId?: string;
supportedAssets?: string;
method?: string;
};
fallbackMessage?: string;
debugLogger?: ErrorHandlerDebugLogger;
}
/**
* Centralized handler for PerpsController errors with context-aware parameter mapping
* @param params - Object containing error, optional context, optional fallback message, and optional debug logger
* @returns Localized error message with proper interpolation
*
* @example
* const errorMessage = handlePerpsError({
* error: depositResult.error,
* context: { token: 'USDT' },
* fallbackMessage: 'Deposit failed'
* });
*/
export function handlePerpsError(params: HandlePerpsErrorParams): string {
const { error, context, fallbackMessage, debugLogger } = params;
// Handle null/undefined errors with fallback
if (!error) {
return fallbackMessage || strings('perps.errors.unknownError');
}
// Extract error string from Error objects or use as-is
let errorString: string | null = null;
if (error instanceof Error) {
errorString = error.message;
} else if (typeof error === 'string') {
errorString = error;
}
// Log error for debugging (without event tracking)
debugLogger?.log('PerpsErrorHandler: Error encountered', {
errorMessage: errorString,
context,
stack: error instanceof Error ? error.stack : undefined,
});
// Check if it's a Core PerpsController or Perps Provider error code
if (
errorString &&
Object.values(PERPS_ERROR_CODES).includes(errorString as PerpsErrorCode)
) {
// Map error codes to their required parameters
const errorParams: Record<string, unknown> = {};
switch (errorString) {
case PERPS_ERROR_CODES.TOKEN_NOT_SUPPORTED:
errorParams.token = context?.token || 'Unknown';
break;
case PERPS_ERROR_CODES.PROVIDER_NOT_AVAILABLE:
errorParams.providerId = context?.providerId || 'Unknown';
break;
// Add other error codes that need parameters as they arise
default:
// Pass through any provided context as-is for other errors
Object.assign(errorParams, context || {});
break;
}
const i18nKey = ERROR_CODE_TO_I18N_KEY[errorString as PerpsErrorCode];
return strings(i18nKey, errorParams);
}
// Try pattern matching for API error messages
if (errorString) {
const matchedErrorCode = matchApiErrorPattern(errorString);
if (matchedErrorCode) {
debugLogger?.log('PerpsErrorHandler: Matched error pattern', {
originalError: errorString,
matchedCode: matchedErrorCode,
});
const i18nKey = ERROR_CODE_TO_I18N_KEY[matchedErrorCode];
// Pass through any provided context for interpolation
return strings(i18nKey, context || {});
}
}
// For any other error/error string that was not matched, use fallback
// Important: Always prefer fallback over raw error strings for better UX
if (fallbackMessage) {
debugLogger?.log('PerpsErrorHandler: Using fallback message', {
originalError: errorString,
fallbackMessage,
});
return fallbackMessage;
}
// Last resort: return the generic unknown error message
// Avoid showing raw technical error strings to users
debugLogger?.log('PerpsErrorHandler: No match found, using generic error', {
originalError: errorString,
});
return strings('perps.errors.unknownError');
}