|
1 | | -import { ErrorCode } from './hardware-errors-enums'; |
| 1 | +import type { ErrorMapping } from './hardware-error-mappings'; |
| 2 | +import { Category, ErrorCode, Severity } from './hardware-errors-enums'; |
2 | 3 |
|
3 | 4 | /** |
4 | | - * DMK (Device Management Kit) `_tag`-based error name mappings. |
| 5 | + * Full DMK (Device Management Kit) `_tag`-based error mappings. |
5 | 6 | * |
6 | 7 | * DMK is Ledger's newer SDK. Unlike legacy `@ledgerhq/errors`, which identify |
7 | 8 | * errors via the standard `error.name` property, DMK errors carry a |
8 | 9 | * non-standard `_tag` string (e.g. `'DeviceSessionNotFound'`, |
9 | 10 | * `'DeviceLockedError'`). Tag values are looked up in this mapping to resolve |
10 | | - * the corresponding `ErrorCode`. |
| 11 | + * the full {@link ErrorMapping} (code, message, severity, category, |
| 12 | + * userMessage). |
| 13 | + * |
| 14 | + * This is the single source of truth for DMK tag → error details. The |
| 15 | + * code-only {@link DMK_ERROR_TAG_MAPPINGS} is derived from this object so |
| 16 | + * consumers that only need the `ErrorCode` (e.g. MetaMask Mobile) can use the |
| 17 | + * simpler mapping without duplicating data. |
11 | 18 | * |
12 | 19 | * These mappings are shared with legacy error names in consumers (e.g. |
13 | 20 | * MetaMask Mobile) since both map to the same `ErrorCode` values. |
14 | 21 | */ |
15 | | -export const DMK_ERROR_TAG_MAPPINGS: Record<string, ErrorCode> = { |
16 | | - DeviceSessionNotFound: ErrorCode.DeviceDisconnected, |
17 | | - ConnectionOpeningError: ErrorCode.BluetoothConnectionFailed, |
18 | | - DeviceDisconnectedWhileSendingError: ErrorCode.DeviceDisconnected, |
19 | | - DeviceDisconnectedBeforeSendingApdu: ErrorCode.DeviceDisconnected, |
20 | | - DeviceLockedError: ErrorCode.AuthenticationDeviceLocked, |
21 | | - DeviceNotConnectedError: ErrorCode.DeviceDisconnected, |
22 | | - SessionRefresherError: ErrorCode.DeviceDisconnected, |
| 22 | +export const DMK_ERROR_MAPPINGS: Record<string, ErrorMapping> = { |
| 23 | + DeviceSessionNotFound: { |
| 24 | + code: ErrorCode.DeviceDisconnected, |
| 25 | + message: 'DMK device session not found', |
| 26 | + severity: Severity.Err, |
| 27 | + category: Category.Connection, |
| 28 | + userMessage: |
| 29 | + 'Your Ledger device was disconnected. Please reconnect and try again.', |
| 30 | + }, |
| 31 | + ConnectionOpeningError: { |
| 32 | + code: ErrorCode.BluetoothConnectionFailed, |
| 33 | + message: 'DMK connection failed to open', |
| 34 | + severity: Severity.Err, |
| 35 | + category: Category.Connection, |
| 36 | + userMessage: |
| 37 | + 'Failed to connect to your Ledger device. Please make sure it is nearby and try again.', |
| 38 | + }, |
| 39 | + DeviceDisconnectedWhileSendingError: { |
| 40 | + code: ErrorCode.DeviceDisconnected, |
| 41 | + message: 'DMK device disconnected while sending', |
| 42 | + severity: Severity.Err, |
| 43 | + category: Category.Connection, |
| 44 | + userMessage: |
| 45 | + 'Your Ledger device was disconnected. Please reconnect and try again.', |
| 46 | + }, |
| 47 | + DeviceDisconnectedBeforeSendingApdu: { |
| 48 | + code: ErrorCode.DeviceDisconnected, |
| 49 | + message: 'DMK device disconnected before sending', |
| 50 | + severity: Severity.Err, |
| 51 | + category: Category.Connection, |
| 52 | + userMessage: |
| 53 | + 'Your Ledger device was disconnected. Please reconnect and try again.', |
| 54 | + }, |
| 55 | + DeviceLockedError: { |
| 56 | + code: ErrorCode.AuthenticationDeviceLocked, |
| 57 | + message: 'DMK device locked', |
| 58 | + severity: Severity.Err, |
| 59 | + category: Category.Authentication, |
| 60 | + userMessage: 'Please unlock your Ledger device to continue.', |
| 61 | + }, |
| 62 | + DeviceNotConnectedError: { |
| 63 | + code: ErrorCode.DeviceDisconnected, |
| 64 | + message: 'DMK device not connected', |
| 65 | + severity: Severity.Err, |
| 66 | + category: Category.Connection, |
| 67 | + userMessage: |
| 68 | + 'Your Ledger device is not connected. Please connect and try again.', |
| 69 | + }, |
| 70 | + SessionRefresherError: { |
| 71 | + code: ErrorCode.DeviceDisconnected, |
| 72 | + message: 'DMK session refresh failed', |
| 73 | + severity: Severity.Err, |
| 74 | + category: Category.Connection, |
| 75 | + userMessage: |
| 76 | + 'Your Ledger device session has expired. Please reconnect and try again.', |
| 77 | + }, |
23 | 78 | }; |
24 | 79 |
|
| 80 | +/** |
| 81 | + * DMK `_tag`-to-`ErrorCode` mappings. |
| 82 | + * |
| 83 | + * Derived from {@link DMK_ERROR_MAPPINGS} so there is a single source of truth |
| 84 | + * for tag → code resolution. Consumers that only need the numeric `ErrorCode` |
| 85 | + * (e.g. MetaMask Mobile) can use this lightweight mapping directly. |
| 86 | + */ |
| 87 | +export const DMK_ERROR_TAG_MAPPINGS: Record<string, ErrorCode> = |
| 88 | + Object.fromEntries( |
| 89 | + Object.entries(DMK_ERROR_MAPPINGS).map(([tag, { code }]) => [tag, code]), |
| 90 | + ); |
| 91 | + |
25 | 92 | /** |
26 | 93 | * DMK-specific message patterns for error parsing. |
27 | 94 | * |
|
0 commit comments