diff --git a/libs/ledger-live-common/src/deviceSDK/transports/core.ts b/libs/ledger-live-common/src/deviceSDK/transports/core.ts index 2dc535492e86..5c342ad853ef 100644 --- a/libs/ledger-live-common/src/deviceSDK/transports/core.ts +++ b/libs/ledger-live-common/src/deviceSDK/transports/core.ts @@ -6,6 +6,7 @@ import { BluetoothRequired, CantOpenDevice, DeviceHalted, + DeviceMangementKitError, FirmwareOrAppUpdateRequired, PairingFailed, PeerRemovedPairing, @@ -266,6 +267,15 @@ const initialErrorRemapping = (error: unknown, context?: TraceContext) => { } } + // HANDLE DMK ERRORS + if (error && typeof error === "object" && "_tag" in error) { + const message = + "originalError" in error && typeof error.originalError === "object" + ? ((error.originalError as Error).message as string) + : (error._tag as string); + mappedError = new DeviceMangementKitError(error._tag as string, message); + } + trace({ type: LOG_TYPE, message: `Initial error remapping: ${error}`, diff --git a/libs/ledger-live-common/src/hw/deviceAccess.ts b/libs/ledger-live-common/src/hw/deviceAccess.ts index 7baa4d56ebd5..345fd77266df 100644 --- a/libs/ledger-live-common/src/hw/deviceAccess.ts +++ b/libs/ledger-live-common/src/hw/deviceAccess.ts @@ -14,6 +14,7 @@ import { DeviceHalted, PeerRemovedPairing, PairingFailed, + DeviceMangementKitError, } from "@ledgerhq/errors"; import { LocalTracer, TraceContext, trace } from "@ledgerhq/logs"; import { getEnv } from "@ledgerhq/live-env"; @@ -32,6 +33,15 @@ const initialErrorRemapping = (error: unknown, context?: TraceContext) => { } } + // HANDLE DMK ERRORS + if (error && typeof error === "object" && "_tag" in error) { + const message = + "originalError" in error && typeof error.originalError === "object" + ? ((error.originalError as Error).message as string) + : (error._tag as string); + mappedError = new DeviceMangementKitError(error._tag as string, message); + } + trace({ type: LOG_TYPE, message: `Initial error remapping: ${error}`, @@ -46,6 +56,7 @@ let errorRemapping = e => throwError(() => e); export const setErrorRemapping = (f: (arg0: Error) => Observable): void => { errorRemapping = f; }; + const never = new Promise(() => {}); /** diff --git a/libs/ledgerjs/packages/errors/src/index.ts b/libs/ledgerjs/packages/errors/src/index.ts index 992aed2c31ce..6ad0e6baf8ff 100644 --- a/libs/ledgerjs/packages/errors/src/index.ts +++ b/libs/ledgerjs/packages/errors/src/index.ts @@ -378,6 +378,14 @@ export class LockedDeviceError extends TransportStatusError { } } +export class DeviceMangementKitError extends Error { + constructor(name: string, message: string) { + super(message); + this.name = name; + Object.setPrototypeOf(this, DeviceMangementKitError.prototype); + } +} + // Represents the type of the class TransportStatusError and its children export type TransportStatusErrorClassType = typeof TransportStatusError | typeof LockedDeviceError;