@metamask/hw-wallet-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +15 -0
- package/dist/hardware-error-mappings.cjs +163 -0
- package/dist/hardware-error-mappings.cjs.map +1 -0
- package/dist/hardware-error-mappings.d.cts +160 -0
- package/dist/hardware-error-mappings.d.cts.map +1 -0
- package/dist/hardware-error-mappings.d.mts +160 -0
- package/dist/hardware-error-mappings.d.mts.map +1 -0
- package/dist/hardware-error-mappings.mjs +160 -0
- package/dist/hardware-error-mappings.mjs.map +1 -0
- package/dist/hardware-error.cjs +142 -0
- package/dist/hardware-error.cjs.map +1 -0
- package/dist/hardware-error.d.cts +61 -0
- package/dist/hardware-error.d.cts.map +1 -0
- package/dist/hardware-error.d.mts +61 -0
- package/dist/hardware-error.d.mts.map +1 -0
- package/dist/hardware-error.mjs +138 -0
- package/dist/hardware-error.mjs.map +1 -0
- package/dist/hardware-errors-enums.cjs +81 -0
- package/dist/hardware-errors-enums.cjs.map +1 -0
- package/dist/hardware-errors-enums.d.cts +62 -0
- package/dist/hardware-errors-enums.d.cts.map +1 -0
- package/dist/hardware-errors-enums.d.mts +62 -0
- package/dist/hardware-errors-enums.d.mts.map +1 -0
- package/dist/hardware-errors-enums.mjs +78 -0
- package/dist/hardware-errors-enums.mjs.map +1 -0
- package/dist/index.cjs +20 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +4 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +83 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { ErrorCode, Severity, Category } from "./hardware-errors-enums.mjs";
|
|
2
|
+
export const LEDGER_ERROR_MAPPINGS = {
|
|
3
|
+
'0x9000': {
|
|
4
|
+
code: ErrorCode.Success,
|
|
5
|
+
message: 'Operation successful',
|
|
6
|
+
severity: Severity.Info,
|
|
7
|
+
category: Category.Success,
|
|
8
|
+
},
|
|
9
|
+
'0x6300': {
|
|
10
|
+
code: ErrorCode.AuthenticationFailed,
|
|
11
|
+
message: 'Authentication failed',
|
|
12
|
+
severity: Severity.Err,
|
|
13
|
+
category: Category.Authentication,
|
|
14
|
+
userMessage: 'Authentication failed. Please verify your credentials.',
|
|
15
|
+
},
|
|
16
|
+
'0x63c0': {
|
|
17
|
+
code: ErrorCode.AuthenticationPinAttemptsRemaining,
|
|
18
|
+
message: 'PIN attempts remaining',
|
|
19
|
+
severity: Severity.Warning,
|
|
20
|
+
category: Category.Authentication,
|
|
21
|
+
userMessage: 'Incorrect PIN. Please try again.',
|
|
22
|
+
},
|
|
23
|
+
'0x6982': {
|
|
24
|
+
code: ErrorCode.AuthenticationSecurityCondition,
|
|
25
|
+
message: 'Security conditions not satisfied',
|
|
26
|
+
severity: Severity.Err,
|
|
27
|
+
category: Category.Authentication,
|
|
28
|
+
userMessage: 'Device is locked or access rights are insufficient. Please unlock your device.',
|
|
29
|
+
},
|
|
30
|
+
'0x6985': {
|
|
31
|
+
code: ErrorCode.UserRejected,
|
|
32
|
+
message: 'User rejected action on device',
|
|
33
|
+
severity: Severity.Warning,
|
|
34
|
+
category: Category.UserAction,
|
|
35
|
+
userMessage: 'Transaction was rejected. Please approve on your device to continue.',
|
|
36
|
+
},
|
|
37
|
+
'0x9804': {
|
|
38
|
+
code: ErrorCode.AuthenticationSecurityCondition,
|
|
39
|
+
message: 'App update required',
|
|
40
|
+
severity: Severity.Err,
|
|
41
|
+
category: Category.Authentication,
|
|
42
|
+
userMessage: 'Please update your Ledger app to continue.',
|
|
43
|
+
},
|
|
44
|
+
'0x9808': {
|
|
45
|
+
code: ErrorCode.AuthenticationFailed,
|
|
46
|
+
message: 'Contradiction in secret code status',
|
|
47
|
+
severity: Severity.Err,
|
|
48
|
+
category: Category.Authentication,
|
|
49
|
+
},
|
|
50
|
+
'0x9840': {
|
|
51
|
+
code: ErrorCode.AuthenticationDeviceBlocked,
|
|
52
|
+
message: 'Code blocked',
|
|
53
|
+
severity: Severity.Critical,
|
|
54
|
+
category: Category.Authentication,
|
|
55
|
+
userMessage: 'Your device is blocked due to too many failed attempts. Please follow device recovery procedures.',
|
|
56
|
+
},
|
|
57
|
+
'0x650f': {
|
|
58
|
+
code: ErrorCode.ConnectionClosed,
|
|
59
|
+
message: 'App closed or connection issue',
|
|
60
|
+
severity: Severity.Err,
|
|
61
|
+
category: Category.Connection,
|
|
62
|
+
userMessage: 'Connection lost or app closed. Please open the corresponding app on your Ledger device.',
|
|
63
|
+
},
|
|
64
|
+
'0x5515': {
|
|
65
|
+
code: ErrorCode.AuthenticationDeviceLocked,
|
|
66
|
+
message: 'Device is locked',
|
|
67
|
+
severity: Severity.Err,
|
|
68
|
+
category: Category.Authentication,
|
|
69
|
+
userMessage: 'Please unlock your Ledger device to continue.',
|
|
70
|
+
},
|
|
71
|
+
'0x5501': {
|
|
72
|
+
code: ErrorCode.UserRejected,
|
|
73
|
+
message: 'User refused on device',
|
|
74
|
+
severity: Severity.Warning,
|
|
75
|
+
category: Category.UserAction,
|
|
76
|
+
userMessage: 'Operation was rejected. Please approve on your device to continue.',
|
|
77
|
+
},
|
|
78
|
+
'0x6a80': {
|
|
79
|
+
code: ErrorCode.DeviceStateBlindSignNotSupported,
|
|
80
|
+
message: 'Blind signing not supported',
|
|
81
|
+
severity: Severity.Err,
|
|
82
|
+
category: Category.DeviceState,
|
|
83
|
+
userMessage: 'Blind signing is not supported on this device.',
|
|
84
|
+
},
|
|
85
|
+
'0x6d00': {
|
|
86
|
+
code: ErrorCode.DeviceStateOnlyV4Supported,
|
|
87
|
+
message: 'Ledger Only V4 supported',
|
|
88
|
+
severity: Severity.Err,
|
|
89
|
+
category: Category.DeviceState,
|
|
90
|
+
userMessage: 'Only V4 is supported on this device.',
|
|
91
|
+
},
|
|
92
|
+
'0x6e00': {
|
|
93
|
+
code: ErrorCode.DeviceStateEthAppClosed,
|
|
94
|
+
message: 'Ethereum app closed',
|
|
95
|
+
severity: Severity.Err,
|
|
96
|
+
category: Category.DeviceState,
|
|
97
|
+
userMessage: 'Ethereum app is closed. Please open it to continue.',
|
|
98
|
+
},
|
|
99
|
+
'0x6501': {
|
|
100
|
+
code: ErrorCode.DeviceStateEthAppOutOfDate,
|
|
101
|
+
message: 'Ethereum app out of date',
|
|
102
|
+
severity: Severity.Err,
|
|
103
|
+
category: Category.DeviceState,
|
|
104
|
+
userMessage: 'Ethereum app is out of date. Please update it to continue.',
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
export const BLE_ERROR_MAPPINGS = {
|
|
108
|
+
BLUETOOTH_PERMISSION_DENIED: {
|
|
109
|
+
code: ErrorCode.PermissionBluetoothDenied,
|
|
110
|
+
message: 'Bluetooth permission denied',
|
|
111
|
+
severity: Severity.Err,
|
|
112
|
+
category: Category.Configuration,
|
|
113
|
+
userMessage: 'Bluetooth permission is required to connect to your hardware wallet. Please enable it in your device settings.',
|
|
114
|
+
},
|
|
115
|
+
LOCATION_PERMISSION_DENIED: {
|
|
116
|
+
code: ErrorCode.PermissionLocationDenied,
|
|
117
|
+
message: 'Location permission denied',
|
|
118
|
+
severity: Severity.Err,
|
|
119
|
+
category: Category.Configuration,
|
|
120
|
+
userMessage: 'Location permission is required for Bluetooth scanning on Android. Please enable it in your device settings.',
|
|
121
|
+
},
|
|
122
|
+
NEARBY_DEVICES_PERMISSION_DENIED: {
|
|
123
|
+
code: ErrorCode.PermissionNearbyDevicesDenied,
|
|
124
|
+
message: 'Nearby devices permission denied',
|
|
125
|
+
severity: Severity.Err,
|
|
126
|
+
category: Category.Configuration,
|
|
127
|
+
userMessage: 'Nearby devices permission is required to scan for your hardware wallet. Please enable it in your device settings.',
|
|
128
|
+
},
|
|
129
|
+
BLUETOOTH_DISABLED: {
|
|
130
|
+
code: ErrorCode.BluetoothDisabled,
|
|
131
|
+
message: 'Bluetooth is turned off',
|
|
132
|
+
severity: Severity.Warning,
|
|
133
|
+
category: Category.Connection,
|
|
134
|
+
userMessage: 'Bluetooth is turned off. Please enable Bluetooth to connect to your hardware wallet.',
|
|
135
|
+
},
|
|
136
|
+
BLUETOOTH_SCAN_FAILED: {
|
|
137
|
+
code: ErrorCode.BluetoothScanFailed,
|
|
138
|
+
message: 'Bluetooth scanning failed',
|
|
139
|
+
severity: Severity.Err,
|
|
140
|
+
category: Category.Connection,
|
|
141
|
+
userMessage: 'Unable to scan for Bluetooth devices. Please try again.',
|
|
142
|
+
},
|
|
143
|
+
BLUETOOTH_CONNECTION_FAILED: {
|
|
144
|
+
code: ErrorCode.BluetoothConnectionFailed,
|
|
145
|
+
message: 'Bluetooth connection failed',
|
|
146
|
+
severity: Severity.Err,
|
|
147
|
+
category: Category.Connection,
|
|
148
|
+
userMessage: 'Failed to connect via Bluetooth. Please make sure your device is nearby and try again.',
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
export const MOBILE_ERROR_MAPPINGS = {
|
|
152
|
+
NOT_SUPPORTED: {
|
|
153
|
+
code: ErrorCode.MobileNotSupported,
|
|
154
|
+
message: 'Operation not supported on mobile',
|
|
155
|
+
severity: Severity.Err,
|
|
156
|
+
category: Category.DeviceState,
|
|
157
|
+
userMessage: 'This operation is not supported on mobile devices.',
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
//# sourceMappingURL=hardware-error-mappings.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hardware-error-mappings.mjs","sourceRoot":"","sources":["../src/hardware-error-mappings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,oCAAgC;AAExE,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,QAAQ,EAAE;QACR,IAAI,EAAE,SAAS,CAAC,OAAO;QACvB,OAAO,EAAE,sBAAsB;QAC/B,QAAQ,EAAE,QAAQ,CAAC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AAE,QAAQ,CAAC,UAAU;QAC7B,WAAW,EAAE,yDAAyD;KACvE;IACD,2BAA2B,EAAE;QAC3B,IAAI,EAAE,SAAS,CAAC,yBAAyB;QACzC,OAAO,EAAE,6BAA6B;QACtC,QAAQ,EAAE,QAAQ,CAAC,GAAG;QACtB,QAAQ,EAAE,QAAQ,CAAC,UAAU;QAC7B,WAAW,EACT,wFAAwF;KAC3F;CACF,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,aAAa,EAAE;QACb,IAAI,EAAE,SAAS,CAAC,kBAAkB;QAClC,OAAO,EAAE,mCAAmC;QAC5C,QAAQ,EAAE,QAAQ,CAAC,GAAG;QACtB,QAAQ,EAAE,QAAQ,CAAC,WAAW;QAC9B,WAAW,EAAE,oDAAoD;KAClE;CACF,CAAC","sourcesContent":["import { ErrorCode, Severity, Category } from './hardware-errors-enums';\n\nexport const LEDGER_ERROR_MAPPINGS = {\n '0x9000': {\n code: ErrorCode.Success,\n message: 'Operation successful',\n severity: Severity.Info,\n category: Category.Success,\n },\n '0x6300': {\n code: ErrorCode.AuthenticationFailed,\n message: 'Authentication failed',\n severity: Severity.Err,\n category: Category.Authentication,\n userMessage: 'Authentication failed. Please verify your credentials.',\n },\n '0x63c0': {\n code: ErrorCode.AuthenticationPinAttemptsRemaining,\n message: 'PIN attempts remaining',\n severity: Severity.Warning,\n category: Category.Authentication,\n userMessage: 'Incorrect PIN. Please try again.',\n },\n '0x6982': {\n code: ErrorCode.AuthenticationSecurityCondition,\n message: 'Security conditions not satisfied',\n severity: Severity.Err,\n category: Category.Authentication,\n\n userMessage:\n 'Device is locked or access rights are insufficient. Please unlock your device.',\n },\n '0x6985': {\n code: ErrorCode.UserRejected,\n message: 'User rejected action on device',\n severity: Severity.Warning,\n category: Category.UserAction,\n\n userMessage:\n 'Transaction was rejected. Please approve on your device to continue.',\n },\n '0x9804': {\n code: ErrorCode.AuthenticationSecurityCondition,\n message: 'App update required',\n severity: Severity.Err,\n category: Category.Authentication,\n\n userMessage: 'Please update your Ledger app to continue.',\n },\n '0x9808': {\n code: ErrorCode.AuthenticationFailed,\n message: 'Contradiction in secret code status',\n severity: Severity.Err,\n category: Category.Authentication,\n },\n '0x9840': {\n code: ErrorCode.AuthenticationDeviceBlocked,\n message: 'Code blocked',\n severity: Severity.Critical,\n category: Category.Authentication,\n\n userMessage:\n 'Your device is blocked due to too many failed attempts. Please follow device recovery procedures.',\n },\n '0x650f': {\n code: ErrorCode.ConnectionClosed,\n message: 'App closed or connection issue',\n severity: Severity.Err,\n category: Category.Connection,\n userMessage:\n 'Connection lost or app closed. Please open the corresponding app on your Ledger device.',\n },\n '0x5515': {\n code: ErrorCode.AuthenticationDeviceLocked,\n message: 'Device is locked',\n severity: Severity.Err,\n category: Category.Authentication,\n userMessage: 'Please unlock your Ledger device to continue.',\n },\n '0x5501': {\n code: ErrorCode.UserRejected,\n message: 'User refused on device',\n severity: Severity.Warning,\n category: Category.UserAction,\n userMessage:\n 'Operation was rejected. Please approve on your device to continue.',\n },\n '0x6a80': {\n code: ErrorCode.DeviceStateBlindSignNotSupported,\n message: 'Blind signing not supported',\n severity: Severity.Err,\n category: Category.DeviceState,\n userMessage: 'Blind signing is not supported on this device.',\n },\n '0x6d00': {\n code: ErrorCode.DeviceStateOnlyV4Supported,\n message: 'Ledger Only V4 supported',\n severity: Severity.Err,\n category: Category.DeviceState,\n userMessage: 'Only V4 is supported on this device.',\n },\n '0x6e00': {\n code: ErrorCode.DeviceStateEthAppClosed,\n message: 'Ethereum app closed',\n severity: Severity.Err,\n category: Category.DeviceState,\n userMessage: 'Ethereum app is closed. Please open it to continue.',\n },\n '0x6501': {\n code: ErrorCode.DeviceStateEthAppOutOfDate,\n message: 'Ethereum app out of date',\n severity: Severity.Err,\n category: Category.DeviceState,\n userMessage: 'Ethereum app is out of date. Please update it to continue.',\n },\n};\n\nexport const BLE_ERROR_MAPPINGS = {\n BLUETOOTH_PERMISSION_DENIED: {\n code: ErrorCode.PermissionBluetoothDenied,\n message: 'Bluetooth permission denied',\n severity: Severity.Err,\n category: Category.Configuration,\n userMessage:\n 'Bluetooth permission is required to connect to your hardware wallet. Please enable it in your device settings.',\n },\n LOCATION_PERMISSION_DENIED: {\n code: ErrorCode.PermissionLocationDenied,\n message: 'Location permission denied',\n severity: Severity.Err,\n category: Category.Configuration,\n userMessage:\n 'Location permission is required for Bluetooth scanning on Android. Please enable it in your device settings.',\n },\n NEARBY_DEVICES_PERMISSION_DENIED: {\n code: ErrorCode.PermissionNearbyDevicesDenied,\n message: 'Nearby devices permission denied',\n severity: Severity.Err,\n category: Category.Configuration,\n userMessage:\n 'Nearby devices permission is required to scan for your hardware wallet. Please enable it in your device settings.',\n },\n BLUETOOTH_DISABLED: {\n code: ErrorCode.BluetoothDisabled,\n message: 'Bluetooth is turned off',\n severity: Severity.Warning,\n category: Category.Connection,\n userMessage:\n 'Bluetooth is turned off. Please enable Bluetooth to connect to your hardware wallet.',\n },\n BLUETOOTH_SCAN_FAILED: {\n code: ErrorCode.BluetoothScanFailed,\n message: 'Bluetooth scanning failed',\n severity: Severity.Err,\n category: Category.Connection,\n userMessage: 'Unable to scan for Bluetooth devices. Please try again.',\n },\n BLUETOOTH_CONNECTION_FAILED: {\n code: ErrorCode.BluetoothConnectionFailed,\n message: 'Bluetooth connection failed',\n severity: Severity.Err,\n category: Category.Connection,\n userMessage:\n 'Failed to connect via Bluetooth. Please make sure your device is nearby and try again.',\n },\n};\n\nexport const MOBILE_ERROR_MAPPINGS = {\n NOT_SUPPORTED: {\n code: ErrorCode.MobileNotSupported,\n message: 'Operation not supported on mobile',\n severity: Severity.Err,\n category: Category.DeviceState,\n userMessage: 'This operation is not supported on mobile devices.',\n },\n};\n"]}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var _HardwareWalletError_instances, _HardwareWalletError_getErrorPrefix;
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.HardwareWalletError = void 0;
|
|
10
|
+
const hardware_errors_enums_1 = require("./hardware-errors-enums.cjs");
|
|
11
|
+
/**
|
|
12
|
+
* Generates a unique error ID using timestamp and random values.
|
|
13
|
+
*
|
|
14
|
+
* @returns A unique error ID string.
|
|
15
|
+
*/
|
|
16
|
+
function generateErrorId() {
|
|
17
|
+
const timestamp = Date.now().toString(36);
|
|
18
|
+
// Random string will be formatted as: 0.fa4dmg7flr8, so we skip 0. part.
|
|
19
|
+
const randomPart = Math.random().toString(36).substring(2, 9);
|
|
20
|
+
return `err_${timestamp}_${randomPart}`;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Gets the human-readable name for an error code using enum reverse mapping.
|
|
24
|
+
*
|
|
25
|
+
* @param code - The error code enum value.
|
|
26
|
+
* @returns The string name of the error code, or 'Unknown' if not found.
|
|
27
|
+
*/
|
|
28
|
+
function getErrorCodeName(code) {
|
|
29
|
+
// Numeric enums have a reverse mapping at runtime: ErrorCode[1000] => "AuthFailed"
|
|
30
|
+
return hardware_errors_enums_1.ErrorCode[code] ?? hardware_errors_enums_1.ErrorCode[hardware_errors_enums_1.ErrorCode.Unknown];
|
|
31
|
+
}
|
|
32
|
+
class HardwareWalletError extends Error {
|
|
33
|
+
constructor(message, options) {
|
|
34
|
+
super(message);
|
|
35
|
+
_HardwareWalletError_instances.add(this);
|
|
36
|
+
this.name = 'HardwareWalletError';
|
|
37
|
+
this.id = generateErrorId();
|
|
38
|
+
this.code = options.code;
|
|
39
|
+
this.severity = options.severity;
|
|
40
|
+
this.category = options.category;
|
|
41
|
+
this.userMessage = options.userMessage;
|
|
42
|
+
this.timestamp = new Date();
|
|
43
|
+
this.metadata = options.metadata;
|
|
44
|
+
this.cause = options.cause;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Checks if this error is critical.
|
|
48
|
+
*
|
|
49
|
+
* @returns True if the error is critical, false otherwise.
|
|
50
|
+
*/
|
|
51
|
+
isCritical() {
|
|
52
|
+
return this.severity === hardware_errors_enums_1.Severity.Critical;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Checks if this error is a warning.
|
|
56
|
+
*
|
|
57
|
+
* @returns True if the error is a warning, false otherwise.
|
|
58
|
+
*/
|
|
59
|
+
isWarning() {
|
|
60
|
+
return this.severity === hardware_errors_enums_1.Severity.Warning;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Creates a new error instance with additional metadata.
|
|
64
|
+
*
|
|
65
|
+
* @param additionalMetadata - Additional metadata to merge with existing metadata.
|
|
66
|
+
* @returns A new HardwareWalletError instance with merged metadata.
|
|
67
|
+
*/
|
|
68
|
+
withMetadata(additionalMetadata) {
|
|
69
|
+
const options = {
|
|
70
|
+
code: this.code,
|
|
71
|
+
severity: this.severity,
|
|
72
|
+
category: this.category,
|
|
73
|
+
userMessage: this.userMessage,
|
|
74
|
+
metadata: { ...(this.metadata ?? {}), ...additionalMetadata },
|
|
75
|
+
};
|
|
76
|
+
if (this.cause !== undefined) {
|
|
77
|
+
options.cause = this.cause;
|
|
78
|
+
}
|
|
79
|
+
return new HardwareWalletError(this.message, options);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Serializes the error to a JSON-compatible object.
|
|
83
|
+
* Note: The cause property is serialized if it exists.
|
|
84
|
+
*
|
|
85
|
+
* @returns A JSON-compatible object representation of the error.
|
|
86
|
+
*/
|
|
87
|
+
toJSON() {
|
|
88
|
+
const json = {
|
|
89
|
+
id: this.id,
|
|
90
|
+
name: this.name,
|
|
91
|
+
message: this.message,
|
|
92
|
+
code: this.code,
|
|
93
|
+
severity: this.severity,
|
|
94
|
+
category: this.category,
|
|
95
|
+
userMessage: this.userMessage,
|
|
96
|
+
timestamp: this.timestamp.toISOString(),
|
|
97
|
+
metadata: this.metadata,
|
|
98
|
+
};
|
|
99
|
+
if (this.cause !== undefined) {
|
|
100
|
+
json.cause = {
|
|
101
|
+
name: this.cause.name,
|
|
102
|
+
message: this.cause.message,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return json;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Returns a user-friendly string representation of the error.
|
|
109
|
+
*
|
|
110
|
+
* @returns A user-friendly string representation of the error.
|
|
111
|
+
*/
|
|
112
|
+
toString() {
|
|
113
|
+
return `${__classPrivateFieldGet(this, _HardwareWalletError_instances, "m", _HardwareWalletError_getErrorPrefix).call(this)}: ${this.message}`;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Returns a detailed string representation for debugging.
|
|
117
|
+
*
|
|
118
|
+
* @returns A detailed string representation of the error for debugging.
|
|
119
|
+
*/
|
|
120
|
+
toDetailedString() {
|
|
121
|
+
const details = [
|
|
122
|
+
this.toString(),
|
|
123
|
+
`User Message: ${this.userMessage}`,
|
|
124
|
+
`Severity: ${this.severity}`,
|
|
125
|
+
`Category: ${this.category}`,
|
|
126
|
+
`Timestamp: ${this.timestamp.toISOString()}`,
|
|
127
|
+
];
|
|
128
|
+
if (this.metadata && Object.keys(this.metadata).length > 0) {
|
|
129
|
+
details.push(`Metadata: ${JSON.stringify(this.metadata, null, 2)}`);
|
|
130
|
+
}
|
|
131
|
+
if (this.cause !== undefined) {
|
|
132
|
+
details.push(`Caused by: ${this.cause.message}`);
|
|
133
|
+
}
|
|
134
|
+
return details.join('\n');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.HardwareWalletError = HardwareWalletError;
|
|
138
|
+
_HardwareWalletError_instances = new WeakSet(), _HardwareWalletError_getErrorPrefix = function _HardwareWalletError_getErrorPrefix() {
|
|
139
|
+
const codeName = getErrorCodeName(this.code);
|
|
140
|
+
return `${this.name} [${codeName}:${this.code}]`;
|
|
141
|
+
};
|
|
142
|
+
//# sourceMappingURL=hardware-error.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hardware-error.cjs","sourceRoot":"","sources":["../src/hardware-error.ts"],"names":[],"mappings":";;;;;;;;;AACA,uEAA8D;AAE9D;;;;GAIG;AACH,SAAS,eAAe;IACtB,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC1C,yEAAyE;IACzE,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC9D,OAAO,OAAO,SAAS,IAAI,UAAU,EAAE,CAAC;AAC1C,CAAC;AAED;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,IAAe;IACvC,mFAAmF;IACnF,OAAO,iCAAS,CAAC,IAAI,CAAC,IAAI,iCAAS,CAAC,iCAAS,CAAC,OAAO,CAAC,CAAC;AACzD,CAAC;AAWD,MAAa,mBAAoB,SAAQ,KAAK;IAiB5C,YAAY,OAAe,EAAE,OAAmC;QAC9D,KAAK,CAAC,OAAO,CAAC,CAAC;;QACf,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;QAClC,IAAI,CAAC,EAAE,GAAG,eAAe,EAAE,CAAC;QAC5B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACjC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACjC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;QACvC,IAAI,CAAC,SAAS,GA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,GAAG,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC7C,OAAO,GAAG,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,IAAI,GAAG,CAAC;AACnD,CAAC","sourcesContent":["import type { Category } from './hardware-errors-enums';\nimport { ErrorCode, Severity } from './hardware-errors-enums';\n\n/**\n * Generates a unique error ID using timestamp and random values.\n *\n * @returns A unique error ID string.\n */\nfunction generateErrorId(): string {\n const timestamp = Date.now().toString(36);\n // Random string will be formatted as: 0.fa4dmg7flr8, so we skip 0. part.\n const randomPart = Math.random().toString(36).substring(2, 9);\n return `err_${timestamp}_${randomPart}`;\n}\n\n/**\n * Gets the human-readable name for an error code using enum reverse mapping.\n *\n * @param code - The error code enum value.\n * @returns The string name of the error code, or 'Unknown' if not found.\n */\nfunction getErrorCodeName(code: ErrorCode): string {\n // Numeric enums have a reverse mapping at runtime: ErrorCode[1000] => \"AuthFailed\"\n return ErrorCode[code] ?? ErrorCode[ErrorCode.Unknown];\n}\n\nexport type HardwareWalletErrorOptions = {\n code: ErrorCode;\n severity: Severity;\n category: Category;\n userMessage: string;\n cause?: Error;\n metadata?: Record<string, unknown>;\n};\n\nexport class HardwareWalletError extends Error {\n public readonly id: string;\n\n public readonly code: ErrorCode;\n\n public readonly severity: Severity;\n\n public readonly category: Category;\n\n public readonly userMessage: string;\n\n public readonly timestamp: Date;\n\n public readonly metadata: Record<string, unknown> | undefined;\n\n public readonly cause: Error | undefined;\n\n constructor(message: string, options: HardwareWalletErrorOptions) {\n super(message);\n this.name = 'HardwareWalletError';\n this.id = generateErrorId();\n this.code = options.code;\n this.severity = options.severity;\n this.category = options.category;\n this.userMessage = options.userMessage;\n this.timestamp = new Date();\n this.metadata = options.metadata;\n this.cause = options.cause;\n }\n\n /**\n * Checks if this error is critical.\n *\n * @returns True if the error is critical, false otherwise.\n */\n isCritical(): boolean {\n return this.severity === Severity.Critical;\n }\n\n /**\n * Checks if this error is a warning.\n *\n * @returns True if the error is a warning, false otherwise.\n */\n isWarning(): boolean {\n return this.severity === Severity.Warning;\n }\n\n /**\n * Creates a new error instance with additional metadata.\n *\n * @param additionalMetadata - Additional metadata to merge with existing metadata.\n * @returns A new HardwareWalletError instance with merged metadata.\n */\n withMetadata(\n additionalMetadata: Record<string, unknown>,\n ): HardwareWalletError {\n const options: HardwareWalletErrorOptions = {\n code: this.code,\n severity: this.severity,\n category: this.category,\n userMessage: this.userMessage,\n metadata: { ...(this.metadata ?? {}), ...additionalMetadata },\n };\n\n if (this.cause !== undefined) {\n options.cause = this.cause;\n }\n\n return new HardwareWalletError(this.message, options);\n }\n\n /**\n * Serializes the error to a JSON-compatible object.\n * Note: The cause property is serialized if it exists.\n *\n * @returns A JSON-compatible object representation of the error.\n */\n toJSON(): Record<string, unknown> {\n const json: Record<string, unknown> = {\n id: this.id,\n name: this.name,\n message: this.message,\n code: this.code,\n severity: this.severity,\n category: this.category,\n userMessage: this.userMessage,\n timestamp: this.timestamp.toISOString(),\n metadata: this.metadata,\n };\n\n if (this.cause !== undefined) {\n json.cause = {\n name: this.cause.name,\n message: this.cause.message,\n };\n }\n\n return json;\n }\n\n /**\n * Returns a user-friendly string representation of the error.\n *\n * @returns A user-friendly string representation of the error.\n */\n toString(): string {\n return `${this.#getErrorPrefix()}: ${this.message}`;\n }\n\n /**\n * Returns a detailed string representation for debugging.\n *\n * @returns A detailed string representation of the error for debugging.\n */\n toDetailedString(): string {\n const details = [\n this.toString(),\n `User Message: ${this.userMessage}`,\n `Severity: ${this.severity}`,\n `Category: ${this.category}`,\n `Timestamp: ${this.timestamp.toISOString()}`,\n ];\n\n if (this.metadata && Object.keys(this.metadata).length > 0) {\n details.push(`Metadata: ${JSON.stringify(this.metadata, null, 2)}`);\n }\n\n if (this.cause !== undefined) {\n details.push(`Caused by: ${this.cause.message}`);\n }\n\n return details.join('\\n');\n }\n\n #getErrorPrefix(): string {\n const codeName = getErrorCodeName(this.code);\n return `${this.name} [${codeName}:${this.code}]`;\n }\n}\n"]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Category } from "./hardware-errors-enums.cjs";
|
|
2
|
+
import { ErrorCode, Severity } from "./hardware-errors-enums.cjs";
|
|
3
|
+
export type HardwareWalletErrorOptions = {
|
|
4
|
+
code: ErrorCode;
|
|
5
|
+
severity: Severity;
|
|
6
|
+
category: Category;
|
|
7
|
+
userMessage: string;
|
|
8
|
+
cause?: Error;
|
|
9
|
+
metadata?: Record<string, unknown>;
|
|
10
|
+
};
|
|
11
|
+
export declare class HardwareWalletError extends Error {
|
|
12
|
+
#private;
|
|
13
|
+
readonly id: string;
|
|
14
|
+
readonly code: ErrorCode;
|
|
15
|
+
readonly severity: Severity;
|
|
16
|
+
readonly category: Category;
|
|
17
|
+
readonly userMessage: string;
|
|
18
|
+
readonly timestamp: Date;
|
|
19
|
+
readonly metadata: Record<string, unknown> | undefined;
|
|
20
|
+
readonly cause: Error | undefined;
|
|
21
|
+
constructor(message: string, options: HardwareWalletErrorOptions);
|
|
22
|
+
/**
|
|
23
|
+
* Checks if this error is critical.
|
|
24
|
+
*
|
|
25
|
+
* @returns True if the error is critical, false otherwise.
|
|
26
|
+
*/
|
|
27
|
+
isCritical(): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Checks if this error is a warning.
|
|
30
|
+
*
|
|
31
|
+
* @returns True if the error is a warning, false otherwise.
|
|
32
|
+
*/
|
|
33
|
+
isWarning(): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Creates a new error instance with additional metadata.
|
|
36
|
+
*
|
|
37
|
+
* @param additionalMetadata - Additional metadata to merge with existing metadata.
|
|
38
|
+
* @returns A new HardwareWalletError instance with merged metadata.
|
|
39
|
+
*/
|
|
40
|
+
withMetadata(additionalMetadata: Record<string, unknown>): HardwareWalletError;
|
|
41
|
+
/**
|
|
42
|
+
* Serializes the error to a JSON-compatible object.
|
|
43
|
+
* Note: The cause property is serialized if it exists.
|
|
44
|
+
*
|
|
45
|
+
* @returns A JSON-compatible object representation of the error.
|
|
46
|
+
*/
|
|
47
|
+
toJSON(): Record<string, unknown>;
|
|
48
|
+
/**
|
|
49
|
+
* Returns a user-friendly string representation of the error.
|
|
50
|
+
*
|
|
51
|
+
* @returns A user-friendly string representation of the error.
|
|
52
|
+
*/
|
|
53
|
+
toString(): string;
|
|
54
|
+
/**
|
|
55
|
+
* Returns a detailed string representation for debugging.
|
|
56
|
+
*
|
|
57
|
+
* @returns A detailed string representation of the error for debugging.
|
|
58
|
+
*/
|
|
59
|
+
toDetailedString(): string;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=hardware-error.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hardware-error.d.cts","sourceRoot":"","sources":["../src/hardware-error.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,oCAAgC;AACxD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,oCAAgC;AAyB9D,MAAM,MAAM,0BAA0B,GAAG;IACvC,IAAI,EAAE,SAAS,CAAC;IAChB,QAAQ,EAAE,QAAQ,CAAC;IACnB,QAAQ,EAAE,QAAQ,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC,CAAC;AAEF,qBAAa,mBAAoB,SAAQ,KAAK;;IAC5C,SAAgB,EAAE,EAAE,MAAM,CAAC;IAE3B,SAAgB,IAAI,EAAE,SAAS,CAAC;IAEhC,SAAgB,QAAQ,EAAE,QAAQ,CAAC;IAEnC,SAAgB,QAAQ,EAAE,QAAQ,CAAC;IAEnC,SAAgB,WAAW,EAAE,MAAM,CAAC;IAEpC,SAAgB,SAAS,EAAE,IAAI,CAAC;IAEhC,SAAgB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IAE9D,SAAgB,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;gBAE7B,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,0BAA0B;IAahE;;;;OAIG;IACH,UAAU,IAAI,OAAO;IAIrB;;;;OAIG;IACH,SAAS,IAAI,OAAO;IAIpB;;;;;OAKG;IACH,YAAY,CACV,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC1C,mBAAmB;IAgBtB;;;;;OAKG;IACH,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAuBjC;;;;OAIG;IACH,QAAQ,IAAI,MAAM;IAIlB;;;;OAIG;IACH,gBAAgB,IAAI,MAAM;CAwB3B"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Category } from "./hardware-errors-enums.mjs";
|
|
2
|
+
import { ErrorCode, Severity } from "./hardware-errors-enums.mjs";
|
|
3
|
+
export type HardwareWalletErrorOptions = {
|
|
4
|
+
code: ErrorCode;
|
|
5
|
+
severity: Severity;
|
|
6
|
+
category: Category;
|
|
7
|
+
userMessage: string;
|
|
8
|
+
cause?: Error;
|
|
9
|
+
metadata?: Record<string, unknown>;
|
|
10
|
+
};
|
|
11
|
+
export declare class HardwareWalletError extends Error {
|
|
12
|
+
#private;
|
|
13
|
+
readonly id: string;
|
|
14
|
+
readonly code: ErrorCode;
|
|
15
|
+
readonly severity: Severity;
|
|
16
|
+
readonly category: Category;
|
|
17
|
+
readonly userMessage: string;
|
|
18
|
+
readonly timestamp: Date;
|
|
19
|
+
readonly metadata: Record<string, unknown> | undefined;
|
|
20
|
+
readonly cause: Error | undefined;
|
|
21
|
+
constructor(message: string, options: HardwareWalletErrorOptions);
|
|
22
|
+
/**
|
|
23
|
+
* Checks if this error is critical.
|
|
24
|
+
*
|
|
25
|
+
* @returns True if the error is critical, false otherwise.
|
|
26
|
+
*/
|
|
27
|
+
isCritical(): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Checks if this error is a warning.
|
|
30
|
+
*
|
|
31
|
+
* @returns True if the error is a warning, false otherwise.
|
|
32
|
+
*/
|
|
33
|
+
isWarning(): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Creates a new error instance with additional metadata.
|
|
36
|
+
*
|
|
37
|
+
* @param additionalMetadata - Additional metadata to merge with existing metadata.
|
|
38
|
+
* @returns A new HardwareWalletError instance with merged metadata.
|
|
39
|
+
*/
|
|
40
|
+
withMetadata(additionalMetadata: Record<string, unknown>): HardwareWalletError;
|
|
41
|
+
/**
|
|
42
|
+
* Serializes the error to a JSON-compatible object.
|
|
43
|
+
* Note: The cause property is serialized if it exists.
|
|
44
|
+
*
|
|
45
|
+
* @returns A JSON-compatible object representation of the error.
|
|
46
|
+
*/
|
|
47
|
+
toJSON(): Record<string, unknown>;
|
|
48
|
+
/**
|
|
49
|
+
* Returns a user-friendly string representation of the error.
|
|
50
|
+
*
|
|
51
|
+
* @returns A user-friendly string representation of the error.
|
|
52
|
+
*/
|
|
53
|
+
toString(): string;
|
|
54
|
+
/**
|
|
55
|
+
* Returns a detailed string representation for debugging.
|
|
56
|
+
*
|
|
57
|
+
* @returns A detailed string representation of the error for debugging.
|
|
58
|
+
*/
|
|
59
|
+
toDetailedString(): string;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=hardware-error.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hardware-error.d.mts","sourceRoot":"","sources":["../src/hardware-error.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,oCAAgC;AACxD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,oCAAgC;AAyB9D,MAAM,MAAM,0BAA0B,GAAG;IACvC,IAAI,EAAE,SAAS,CAAC;IAChB,QAAQ,EAAE,QAAQ,CAAC;IACnB,QAAQ,EAAE,QAAQ,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC,CAAC;AAEF,qBAAa,mBAAoB,SAAQ,KAAK;;IAC5C,SAAgB,EAAE,EAAE,MAAM,CAAC;IAE3B,SAAgB,IAAI,EAAE,SAAS,CAAC;IAEhC,SAAgB,QAAQ,EAAE,QAAQ,CAAC;IAEnC,SAAgB,QAAQ,EAAE,QAAQ,CAAC;IAEnC,SAAgB,WAAW,EAAE,MAAM,CAAC;IAEpC,SAAgB,SAAS,EAAE,IAAI,CAAC;IAEhC,SAAgB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IAE9D,SAAgB,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;gBAE7B,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,0BAA0B;IAahE;;;;OAIG;IACH,UAAU,IAAI,OAAO;IAIrB;;;;OAIG;IACH,SAAS,IAAI,OAAO;IAIpB;;;;;OAKG;IACH,YAAY,CACV,kBAAkB,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC1C,mBAAmB;IAgBtB;;;;;OAKG;IACH,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAuBjC;;;;OAIG;IACH,QAAQ,IAAI,MAAM;IAIlB;;;;OAIG;IACH,gBAAgB,IAAI,MAAM;CAwB3B"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _HardwareWalletError_instances, _HardwareWalletError_getErrorPrefix;
|
|
7
|
+
import { ErrorCode, Severity } from "./hardware-errors-enums.mjs";
|
|
8
|
+
/**
|
|
9
|
+
* Generates a unique error ID using timestamp and random values.
|
|
10
|
+
*
|
|
11
|
+
* @returns A unique error ID string.
|
|
12
|
+
*/
|
|
13
|
+
function generateErrorId() {
|
|
14
|
+
const timestamp = Date.now().toString(36);
|
|
15
|
+
// Random string will be formatted as: 0.fa4dmg7flr8, so we skip 0. part.
|
|
16
|
+
const randomPart = Math.random().toString(36).substring(2, 9);
|
|
17
|
+
return `err_${timestamp}_${randomPart}`;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Gets the human-readable name for an error code using enum reverse mapping.
|
|
21
|
+
*
|
|
22
|
+
* @param code - The error code enum value.
|
|
23
|
+
* @returns The string name of the error code, or 'Unknown' if not found.
|
|
24
|
+
*/
|
|
25
|
+
function getErrorCodeName(code) {
|
|
26
|
+
// Numeric enums have a reverse mapping at runtime: ErrorCode[1000] => "AuthFailed"
|
|
27
|
+
return ErrorCode[code] ?? ErrorCode[ErrorCode.Unknown];
|
|
28
|
+
}
|
|
29
|
+
export class HardwareWalletError extends Error {
|
|
30
|
+
constructor(message, options) {
|
|
31
|
+
super(message);
|
|
32
|
+
_HardwareWalletError_instances.add(this);
|
|
33
|
+
this.name = 'HardwareWalletError';
|
|
34
|
+
this.id = generateErrorId();
|
|
35
|
+
this.code = options.code;
|
|
36
|
+
this.severity = options.severity;
|
|
37
|
+
this.category = options.category;
|
|
38
|
+
this.userMessage = options.userMessage;
|
|
39
|
+
this.timestamp = new Date();
|
|
40
|
+
this.metadata = options.metadata;
|
|
41
|
+
this.cause = options.cause;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Checks if this error is critical.
|
|
45
|
+
*
|
|
46
|
+
* @returns True if the error is critical, false otherwise.
|
|
47
|
+
*/
|
|
48
|
+
isCritical() {
|
|
49
|
+
return this.severity === Severity.Critical;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Checks if this error is a warning.
|
|
53
|
+
*
|
|
54
|
+
* @returns True if the error is a warning, false otherwise.
|
|
55
|
+
*/
|
|
56
|
+
isWarning() {
|
|
57
|
+
return this.severity === Severity.Warning;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Creates a new error instance with additional metadata.
|
|
61
|
+
*
|
|
62
|
+
* @param additionalMetadata - Additional metadata to merge with existing metadata.
|
|
63
|
+
* @returns A new HardwareWalletError instance with merged metadata.
|
|
64
|
+
*/
|
|
65
|
+
withMetadata(additionalMetadata) {
|
|
66
|
+
const options = {
|
|
67
|
+
code: this.code,
|
|
68
|
+
severity: this.severity,
|
|
69
|
+
category: this.category,
|
|
70
|
+
userMessage: this.userMessage,
|
|
71
|
+
metadata: { ...(this.metadata ?? {}), ...additionalMetadata },
|
|
72
|
+
};
|
|
73
|
+
if (this.cause !== undefined) {
|
|
74
|
+
options.cause = this.cause;
|
|
75
|
+
}
|
|
76
|
+
return new HardwareWalletError(this.message, options);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Serializes the error to a JSON-compatible object.
|
|
80
|
+
* Note: The cause property is serialized if it exists.
|
|
81
|
+
*
|
|
82
|
+
* @returns A JSON-compatible object representation of the error.
|
|
83
|
+
*/
|
|
84
|
+
toJSON() {
|
|
85
|
+
const json = {
|
|
86
|
+
id: this.id,
|
|
87
|
+
name: this.name,
|
|
88
|
+
message: this.message,
|
|
89
|
+
code: this.code,
|
|
90
|
+
severity: this.severity,
|
|
91
|
+
category: this.category,
|
|
92
|
+
userMessage: this.userMessage,
|
|
93
|
+
timestamp: this.timestamp.toISOString(),
|
|
94
|
+
metadata: this.metadata,
|
|
95
|
+
};
|
|
96
|
+
if (this.cause !== undefined) {
|
|
97
|
+
json.cause = {
|
|
98
|
+
name: this.cause.name,
|
|
99
|
+
message: this.cause.message,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
return json;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Returns a user-friendly string representation of the error.
|
|
106
|
+
*
|
|
107
|
+
* @returns A user-friendly string representation of the error.
|
|
108
|
+
*/
|
|
109
|
+
toString() {
|
|
110
|
+
return `${__classPrivateFieldGet(this, _HardwareWalletError_instances, "m", _HardwareWalletError_getErrorPrefix).call(this)}: ${this.message}`;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Returns a detailed string representation for debugging.
|
|
114
|
+
*
|
|
115
|
+
* @returns A detailed string representation of the error for debugging.
|
|
116
|
+
*/
|
|
117
|
+
toDetailedString() {
|
|
118
|
+
const details = [
|
|
119
|
+
this.toString(),
|
|
120
|
+
`User Message: ${this.userMessage}`,
|
|
121
|
+
`Severity: ${this.severity}`,
|
|
122
|
+
`Category: ${this.category}`,
|
|
123
|
+
`Timestamp: ${this.timestamp.toISOString()}`,
|
|
124
|
+
];
|
|
125
|
+
if (this.metadata && Object.keys(this.metadata).length > 0) {
|
|
126
|
+
details.push(`Metadata: ${JSON.stringify(this.metadata, null, 2)}`);
|
|
127
|
+
}
|
|
128
|
+
if (this.cause !== undefined) {
|
|
129
|
+
details.push(`Caused by: ${this.cause.message}`);
|
|
130
|
+
}
|
|
131
|
+
return details.join('\n');
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
_HardwareWalletError_instances = new WeakSet(), _HardwareWalletError_getErrorPrefix = function _HardwareWalletError_getErrorPrefix() {
|
|
135
|
+
const codeName = getErrorCodeName(this.code);
|
|
136
|
+
return `${this.name} [${codeName}:${this.code}]`;
|
|
137
|
+
};
|
|
138
|
+
//# sourceMappingURL=hardware-error.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hardware-error.mjs","sourceRoot":"","sources":["../src/hardware-error.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,oCAAgC;AAE9D;;;;GAIG;AACH,SAAS,eAAe;IACtB,MAAM,SAAS,GAAG,IAAI,CAAC,GAA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2EAAgB,MAApB,IAAI,CAAkB,KAAK,IAAI,CAAC,OAAO,EAAE,CAAC;IACtD,CAAC;IAED;;;;OAIG;IACH,gBAAgB;QACd,MAAM,OAAO,GAAG;YACd,IAAI,CAAC,QAAQ,EAAE;YACf,iBAAiB,IAAI,CAAC,WAAW,EAAE;YACnC,aAAa,IAAI,CAAC,QAAQ,EAAE;YAC5B,aAAa,IAAI,CAAC,QAAQ,EAAE;YAC5B,cAAc,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE,EAAE;SAC7C,CAAC;QAEF,IAAI,IAAI,CAAC,QAAQ,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC3D,OAAO,CAAC,IAAI,CAAC,aAAa,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;QACtE,CAAC;QAED,IAAI,IAAI,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;YAC7B,OAAO,CAAC,IAAI,CAAC,cAAc,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QACnD,CAAC;QAED,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;CAMF;;IAHG,MAAM,QAAQ,GAAG,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC7C,OAAO,GAAG,IAAI,CAAC,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,IAAI,GAAG,CAAC;AACnD,CAAC","sourcesContent":["import type { Category } from './hardware-errors-enums';\nimport { ErrorCode, Severity } from './hardware-errors-enums';\n\n/**\n * Generates a unique error ID using timestamp and random values.\n *\n * @returns A unique error ID string.\n */\nfunction generateErrorId(): string {\n const timestamp = Date.now().toString(36);\n // Random string will be formatted as: 0.fa4dmg7flr8, so we skip 0. part.\n const randomPart = Math.random().toString(36).substring(2, 9);\n return `err_${timestamp}_${randomPart}`;\n}\n\n/**\n * Gets the human-readable name for an error code using enum reverse mapping.\n *\n * @param code - The error code enum value.\n * @returns The string name of the error code, or 'Unknown' if not found.\n */\nfunction getErrorCodeName(code: ErrorCode): string {\n // Numeric enums have a reverse mapping at runtime: ErrorCode[1000] => \"AuthFailed\"\n return ErrorCode[code] ?? ErrorCode[ErrorCode.Unknown];\n}\n\nexport type HardwareWalletErrorOptions = {\n code: ErrorCode;\n severity: Severity;\n category: Category;\n userMessage: string;\n cause?: Error;\n metadata?: Record<string, unknown>;\n};\n\nexport class HardwareWalletError extends Error {\n public readonly id: string;\n\n public readonly code: ErrorCode;\n\n public readonly severity: Severity;\n\n public readonly category: Category;\n\n public readonly userMessage: string;\n\n public readonly timestamp: Date;\n\n public readonly metadata: Record<string, unknown> | undefined;\n\n public readonly cause: Error | undefined;\n\n constructor(message: string, options: HardwareWalletErrorOptions) {\n super(message);\n this.name = 'HardwareWalletError';\n this.id = generateErrorId();\n this.code = options.code;\n this.severity = options.severity;\n this.category = options.category;\n this.userMessage = options.userMessage;\n this.timestamp = new Date();\n this.metadata = options.metadata;\n this.cause = options.cause;\n }\n\n /**\n * Checks if this error is critical.\n *\n * @returns True if the error is critical, false otherwise.\n */\n isCritical(): boolean {\n return this.severity === Severity.Critical;\n }\n\n /**\n * Checks if this error is a warning.\n *\n * @returns True if the error is a warning, false otherwise.\n */\n isWarning(): boolean {\n return this.severity === Severity.Warning;\n }\n\n /**\n * Creates a new error instance with additional metadata.\n *\n * @param additionalMetadata - Additional metadata to merge with existing metadata.\n * @returns A new HardwareWalletError instance with merged metadata.\n */\n withMetadata(\n additionalMetadata: Record<string, unknown>,\n ): HardwareWalletError {\n const options: HardwareWalletErrorOptions = {\n code: this.code,\n severity: this.severity,\n category: this.category,\n userMessage: this.userMessage,\n metadata: { ...(this.metadata ?? {}), ...additionalMetadata },\n };\n\n if (this.cause !== undefined) {\n options.cause = this.cause;\n }\n\n return new HardwareWalletError(this.message, options);\n }\n\n /**\n * Serializes the error to a JSON-compatible object.\n * Note: The cause property is serialized if it exists.\n *\n * @returns A JSON-compatible object representation of the error.\n */\n toJSON(): Record<string, unknown> {\n const json: Record<string, unknown> = {\n id: this.id,\n name: this.name,\n message: this.message,\n code: this.code,\n severity: this.severity,\n category: this.category,\n userMessage: this.userMessage,\n timestamp: this.timestamp.toISOString(),\n metadata: this.metadata,\n };\n\n if (this.cause !== undefined) {\n json.cause = {\n name: this.cause.name,\n message: this.cause.message,\n };\n }\n\n return json;\n }\n\n /**\n * Returns a user-friendly string representation of the error.\n *\n * @returns A user-friendly string representation of the error.\n */\n toString(): string {\n return `${this.#getErrorPrefix()}: ${this.message}`;\n }\n\n /**\n * Returns a detailed string representation for debugging.\n *\n * @returns A detailed string representation of the error for debugging.\n */\n toDetailedString(): string {\n const details = [\n this.toString(),\n `User Message: ${this.userMessage}`,\n `Severity: ${this.severity}`,\n `Category: ${this.category}`,\n `Timestamp: ${this.timestamp.toISOString()}`,\n ];\n\n if (this.metadata && Object.keys(this.metadata).length > 0) {\n details.push(`Metadata: ${JSON.stringify(this.metadata, null, 2)}`);\n }\n\n if (this.cause !== undefined) {\n details.push(`Caused by: ${this.cause.message}`);\n }\n\n return details.join('\\n');\n }\n\n #getErrorPrefix(): string {\n const codeName = getErrorCodeName(this.code);\n return `${this.name} [${codeName}:${this.code}]`;\n }\n}\n"]}
|