Comprehensive error handling library for the Ledger ecosystem with unified error classes, serialization, and hardware wallet status codes
Error classes for network connectivity, API communication, service availability issues, and database-related operations.
Error classes for general network connectivity and availability issues.
const NetworkDown: CustomErrorFunc;
const WebsocketConnectionError: CustomErrorFunc;
const WebsocketConnectionFailed: CustomErrorFunc;Usage Examples:
import {
NetworkDown,
WebsocketConnectionError,
WebsocketConnectionFailed
} from "@ledgerhq/errors";
// General network unavailability
throw new NetworkDown("Network is currently unavailable", {
service: "Ledger Live backend",
estimatedRestoreTime: "15 minutes",
alternativeAction: "Try offline mode"
});
// WebSocket connection error
throw new WebsocketConnectionError("WebSocket connection encountered an error", {
errorCode: "WS_CONNECTION_LOST",
reconnectAttempt: 3,
maxRetries: 5
});
// WebSocket connection failed to establish
throw new WebsocketConnectionFailed("Failed to establish WebSocket connection", {
endpoint: "wss://api.ledger.com/ws",
reason: "Connection timeout",
suggestedAction: "Check firewall and proxy settings"
});Error classes for Ledger's backend API communication and responses.
const LedgerAPIError: CustomErrorFunc;
const LedgerAPIErrorWithMessage: CustomErrorFunc;
const LedgerAPINotAvailable: CustomErrorFunc;
const LedgerAPI4xx: CustomErrorFunc;
const LedgerAPI5xx: CustomErrorFunc;Usage Examples:
import {
LedgerAPIError,
LedgerAPIErrorWithMessage,
LedgerAPINotAvailable,
LedgerAPI4xx,
LedgerAPI5xx
} from "@ledgerhq/errors";
// General Ledger API error
throw new LedgerAPIError("Ledger API request failed", {
endpoint: "/v1/accounts",
method: "GET",
errorCode: "API_ERROR"
});
// Ledger API error with specific message
throw new LedgerAPIErrorWithMessage("Rate limit exceeded for API requests", {
rateLimitReset: "2023-01-01T13:00:00Z",
requestsRemaining: 0,
maxRequestsPerHour: 1000
});
// Ledger API service unavailable
throw new LedgerAPINotAvailable("Ledger API service is temporarily unavailable", {
maintenanceWindow: "2023-01-01T02:00:00Z - 2023-01-01T04:00:00Z",
statusPage: "https://status.ledger.com"
});
// HTTP 4xx client error from Ledger API
throw new LedgerAPI4xx("Client error from Ledger API", {
statusCode: 404,
endpoint: "/v1/currencies/INVALID",
message: "Currency not found"
});
// HTTP 5xx server error from Ledger API
throw new LedgerAPI5xx("Server error from Ledger API", {
statusCode: 503,
endpoint: "/v1/transactions",
message: "Service temporarily unavailable",
retryAfter: "60 seconds"
});Error classes for local database operations and configuration.
const NoDBPathGiven: CustomErrorFunc;
const DBWrongPassword: CustomErrorFunc;
const DBNotReset: CustomErrorFunc;Usage Examples:
import {
NoDBPathGiven,
DBWrongPassword,
DBNotReset
} from "@ledgerhq/errors";
// Database path not specified
throw new NoDBPathGiven("Database path must be provided", {
configParameter: "dbPath",
examplePath: "/home/user/.config/ledger-live/app.db"
});
// Incorrect database password
throw new DBWrongPassword("Incorrect password for encrypted database", {
attemptsRemaining: 2,
maxAttempts: 3,
lockoutDuration: "5 minutes"
});
// Database not reset when required
throw new DBNotReset("Database reset is required before this operation", {
reason: "Schema version mismatch",
requiredAction: "Backup data and reset database",
backupLocation: "/tmp/ledger-backup"
});Common patterns for handling network and API errors:
import {
NetworkDown,
LedgerAPIError,
LedgerAPI5xx,
WebsocketConnectionFailed
} from "@ledgerhq/errors";
// Retry pattern for network errors
async function withRetry<T>(
operation: () => Promise<T>,
maxRetries: number = 3
): Promise<T> {
let lastError: Error;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await operation();
} catch (error) {
lastError = error;
if (error instanceof NetworkDown ||
error instanceof LedgerAPI5xx ||
error instanceof WebsocketConnectionFailed) {
if (attempt < maxRetries) {
const delay = Math.pow(2, attempt) * 1000; // Exponential backoff
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
}
// Don't retry for client errors
if (error instanceof LedgerAPI4xx) {
throw error;
}
throw error;
}
}
throw lastError;
}
// Usage example
try {
const accountData = await withRetry(() => fetchAccountData(accountId));
} catch (error) {
if (error instanceof LedgerAPINotAvailable) {
console.log("Service maintenance in progress");
} else if (error instanceof NetworkDown) {
console.log("Please check your internet connection");
}
}import {
NetworkDown,
WebsocketConnectionError,
WebsocketConnectionFailed
} from "@ledgerhq/errors";
class ConnectionMonitor {
private reconnectAttempts = 0;
private maxReconnectAttempts = 5;
async handleConnectionError(error: Error): Promise<void> {
if (error instanceof WebsocketConnectionFailed) {
if (this.reconnectAttempts < this.maxReconnectAttempts) {
this.reconnectAttempts++;
const delay = this.reconnectAttempts * 2000;
console.log(`Reconnection attempt ${this.reconnectAttempts} in ${delay}ms`);
setTimeout(() => {
this.attemptReconnection();
}, delay);
} else {
throw new NetworkDown("Max reconnection attempts exceeded");
}
}
}
private async attemptReconnection(): Promise<void> {
// Reconnection logic here
}
}type CustomErrorFunc = (
message?: string,
fields?: { [key: string]: any }
) => void;tessl i tessl/npm-ledgerhq--errors@5.50.0