Comprehensive error handling system for Ledger hardware wallet applications and libraries
npx @tessl/cli install tessl/npm-ledgerhq--errors@6.10.0@ledgerhq/errors provides a comprehensive error handling system for Ledger hardware wallet applications and libraries. It includes a collection of custom error classes specifically designed for Ledger device communication, transport protocols, and blockchain interactions with utilities for creating custom error classes, serializing and deserializing errors for cross-context communication, and maintaining a unified error taxonomy across the Ledger ecosystem.
npm install @ledgerhq/errorsimport { TransportError, TransportStatusError, StatusCodes } from "@ledgerhq/errors";
import { serializeError, deserializeError, createCustomErrorClass } from "@ledgerhq/errors";
import {
NoDBPathGiven, DBWrongPassword, DBNotReset,
EthAppPleaseEnableContractData,
UpdateFetchFileFail, UpdateIncorrectHash, UpdateIncorrectSig
} from "@ledgerhq/errors";For CommonJS:
const { TransportError, TransportStatusError, StatusCodes } = require("@ledgerhq/errors");
const { serializeError, deserializeError, createCustomErrorClass } = require("@ledgerhq/errors");
const {
NoDBPathGiven, DBWrongPassword, DBNotReset,
EthAppPleaseEnableContractData,
UpdateFetchFileFail, UpdateIncorrectHash, UpdateIncorrectSig
} = require("@ledgerhq/errors");import {
TransportError,
TransportStatusError,
StatusCodes,
DeviceNotGenuineError,
serializeError,
deserializeError
} from "@ledgerhq/errors";
// Using specific error classes
try {
// Device operation
} catch (error) {
if (error instanceof DeviceNotGenuineError) {
console.log("Device authenticity check failed");
}
}
// Using transport errors
const transportError = new TransportError("Communication failed", "USB_DISCONNECT");
// Using transport status errors
const statusError = new TransportStatusError(StatusCodes.DEVICE_LOCKED);
// Error serialization for cross-context communication
const serialized = serializeError(error);
const restored = deserializeError(serialized);@ledgerhq/errors is built around several key patterns:
createCustomErrorClass generates custom error types with consistent behaviorTransportError, TransportStatusError)serializeError/deserializeErrorCore utilities for creating custom error classes and handling error serialization across different contexts.
function createCustomErrorClass(name: string): CustomErrorFunc;
function serializeError(value: any): undefined | To | string;
function deserializeError(object: any): Error;
function addCustomErrorDeserializer(
name: string,
deserializer: (obj: any) => any
): void;
type CustomErrorFunc = (
message?: string,
fields?: { [key: string]: any }
) => void;
interface To {
name?: string;
message?: string;
stack?: string;
}Specialized error classes for Ledger device communication, including transport-level failures and device status code errors.
function TransportError(message: string, id: string): void;
function TransportStatusError(statusCode: number): void;
const StatusCodes: {
PIN_REMAINING_ATTEMPTS: number;
INCORRECT_LENGTH: number;
SECURITY_STATUS_NOT_SATISFIED: number;
CONDITIONS_OF_USE_NOT_SATISFIED: number;
OK: number;
// ... 25+ additional status codes
};
function getAltStatusMessage(code: number): string | undefined | null;Error classes for Ledger device management operations including device state validation, connection issues, and hardware-specific errors.
const CantOpenDevice: CustomErrorFunc;
const DeviceNotGenuineError: CustomErrorFunc;
const DeviceOnDashboardExpected: CustomErrorFunc;
const DeviceOnDashboardUnexpected: CustomErrorFunc;
const DeviceInOSUExpected: CustomErrorFunc;
const DeviceHalted: CustomErrorFunc;
const DisconnectedDevice: CustomErrorFunc;
const DisconnectedDeviceDuringOperation: CustomErrorFunc;
// ... 15+ additional device management errorsError classes for user interaction scenarios including user refusals, permission issues, and input validation failures.
const UserRefusedOnDevice: CustomErrorFunc;
const UserRefusedAddress: CustomErrorFunc;
const UserRefusedFirmwareUpdate: CustomErrorFunc;
const UserRefusedAllowManager: CustomErrorFunc;
const UserRefusedDeviceNameChange: CustomErrorFunc;
const NoAccessToCamera: CustomErrorFunc;
const CantScanQRCode: CustomErrorFunc;
const PairingFailed: CustomErrorFunc;
// ... 5+ additional user interaction errorsError classes for account management and balance validation including insufficient funds, account state issues, and transaction validation errors.
const AccountNameRequiredError: CustomErrorFunc;
const AccountNotSupported: CustomErrorFunc;
const NotEnoughBalance: CustomErrorFunc;
const NotEnoughBalanceToDelegate: CustomErrorFunc;
const NotEnoughBalanceInParentAccount: CustomErrorFunc;
const NotEnoughSpendableBalance: CustomErrorFunc;
const NotEnoughBalanceBecauseDestinationNotCreated: CustomErrorFunc;
const WrongDeviceForAccount: CustomErrorFunc;
const NoAddressesFound: CustomErrorFunc;
// ... 5+ additional account and balance errorsError classes for Ledger application management including app installation, dependencies, firmware updates, and manager operations.
const ManagerAppAlreadyInstalledError: CustomErrorFunc;
const ManagerAppRelyOnBTCError: CustomErrorFunc;
const ManagerAppDepInstallRequired: CustomErrorFunc;
const ManagerAppDepUninstallRequired: CustomErrorFunc;
const ManagerDeviceLockedError: CustomErrorFunc;
const ManagerNotEnoughSpaceError: CustomErrorFunc;
const FirmwareNotRecognized: CustomErrorFunc;
const FirmwareOrAppUpdateRequired: CustomErrorFunc;
const UpdateYourApp: CustomErrorFunc;
// ... 10+ additional application and manager errorsApplication and Manager Errors
Error classes for network connectivity, API communication, and external service integration issues.
const NetworkDown: CustomErrorFunc;
const LedgerAPIError: CustomErrorFunc;
const LedgerAPIErrorWithMessage: CustomErrorFunc;
const LedgerAPINotAvailable: CustomErrorFunc;
const LedgerAPI4xx: CustomErrorFunc;
const LedgerAPI5xx: CustomErrorFunc;
const WebsocketConnectionError: CustomErrorFunc;
const WebsocketConnectionFailed: CustomErrorFunc;
// ... 5+ additional network and API errorsError classes for cryptocurrency operations including address validation, transaction processing, fee estimation, and currency-specific issues.
const CurrencyNotSupported: CustomErrorFunc;
const InvalidAddress: CustomErrorFunc;
const InvalidAddressBecauseDestinationIsAlsoSource: CustomErrorFunc;
const InvalidXRPTag: CustomErrorFunc;
const AmountRequired: CustomErrorFunc;
const RecipientRequired: CustomErrorFunc;
const FeeEstimationFailed: CustomErrorFunc;
const FeeRequired: CustomErrorFunc;
const FeeTooHigh: CustomErrorFunc;
const NotEnoughGas: CustomErrorFunc;
const GasLessThanEstimate: CustomErrorFunc;
// ... 10+ additional currency and transaction errorsCurrency and Transaction Errors
Error classes for database operations and configuration issues in Ledger applications.
const NoDBPathGiven: CustomErrorFunc;
const DBWrongPassword: CustomErrorFunc;
const DBNotReset: CustomErrorFunc;