-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patherrors.ts
More file actions
93 lines (85 loc) · 2.72 KB
/
Copy patherrors.ts
File metadata and controls
93 lines (85 loc) · 2.72 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
import {
type ErrorCode,
type Severity,
type Category,
type RetryStrategy,
HardwareWalletError,
HARDWARE_MAPPINGS,
ErrorCode as ErrorCodeEnum,
Severity as SeverityEnum,
Category as CategoryEnum,
RetryStrategy as RetryStrategyEnum,
} from '@metamask/keyring-utils';
type LedgerErrorMapping = {
customCode: ErrorCode;
message: string;
severity: Severity;
category: Category;
retryStrategy: RetryStrategy;
userActionable: boolean;
userMessage?: string;
};
/**
* Factory function to create a HardwareWalletError from a Ledger error code.
*
* @param ledgerErrorCode - The Ledger error code (e.g., '0x6985', '0x5515')
* @param context - Optional additional context to append to the error message
* @returns A LedgerHardwareWalletError instance with mapped error details
*/
export function createLedgerError(
ledgerErrorCode: string,
context?: string,
): HardwareWalletError {
const mappings = HARDWARE_MAPPINGS.ledger.errorMappings as {
[key: string]: LedgerErrorMapping;
};
const errorMapping = mappings[ledgerErrorCode];
if (errorMapping) {
const message = context
? `${errorMapping.message} (${context})`
: errorMapping.message;
return new HardwareWalletError(message, {
code: errorMapping.customCode,
severity: errorMapping.severity,
category: errorMapping.category,
retryStrategy: errorMapping.retryStrategy,
userActionable: errorMapping.userActionable,
userMessage: errorMapping.userMessage ?? '',
});
}
// Fallback for unknown error codes
const fallbackMessage = context
? `Unknown Ledger error: ${ledgerErrorCode} (${context})`
: `Unknown Ledger error: ${ledgerErrorCode}`;
return new HardwareWalletError(fallbackMessage, {
code: ErrorCodeEnum.UNKNOWN_001,
severity: SeverityEnum.ERROR,
category: CategoryEnum.UNKNOWN,
retryStrategy: RetryStrategyEnum.NO_RETRY,
userActionable: false,
userMessage: '',
});
}
/**
* Checks if a Ledger error code exists in the error mappings.
*
* @param ledgerErrorCode - The Ledger error code to check
* @returns True if the error code is mapped, false otherwise
*/
export function isKnownLedgerError(ledgerErrorCode: string): boolean {
return ledgerErrorCode in HARDWARE_MAPPINGS.ledger.errorMappings;
}
/**
* Gets the error mapping details for a Ledger error code without creating an error instance.
*
* @param ledgerErrorCode - The Ledger error code to look up
* @returns The error mapping details or undefined if not found
*/
export function getLedgerErrorMapping(
ledgerErrorCode: string,
): LedgerErrorMapping | undefined {
const mappings = HARDWARE_MAPPINGS.ledger.errorMappings as {
[key: string]: LedgerErrorMapping;
};
return mappings[ledgerErrorCode];
}