-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patherrors.ts
More file actions
69 lines (62 loc) · 2 KB
/
Copy patherrors.ts
File metadata and controls
69 lines (62 loc) · 2 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
import {
ErrorMapping,
ErrorCode,
Severity,
Category,
HardwareWalletError,
LEDGER_ERROR_MAPPINGS,
} from '@metamask/hw-wallet-sdk';
/**
* 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 HardwareWalletError instance with mapped error details
*/
export function createLedgerError(
ledgerErrorCode: string,
context?: string,
): HardwareWalletError {
const errorMapping = getLedgerErrorMapping(ledgerErrorCode);
if (errorMapping) {
const message = context
? `${errorMapping.message} (${context})`
: errorMapping.message;
return new HardwareWalletError(message, {
code: errorMapping.code,
severity: errorMapping.severity,
category: errorMapping.category,
userMessage: errorMapping.userMessage ?? message,
});
}
// Fallback for unknown error codes
const fallbackMessage = context
? `Unknown Ledger error: ${ledgerErrorCode} (${context})`
: `Unknown Ledger error: ${ledgerErrorCode}`;
return new HardwareWalletError(fallbackMessage, {
code: ErrorCode.Unknown,
severity: Severity.Err,
category: Category.Unknown,
userMessage: fallbackMessage,
});
}
/**
* 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 LEDGER_ERROR_MAPPINGS;
}
/**
* 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,
): ErrorMapping | undefined {
return LEDGER_ERROR_MAPPINGS[ledgerErrorCode];
}