UNPKG

@sailboat-computer/data-storage

Version:

Shared data storage library for sailboat computer v3

109 lines (108 loc) 3.19 kB
/** * Error handling and resilience utilities * * This file contains error classes and utility functions for error handling * and resilience patterns like circuit breakers, retries, and timeouts. */ import { CircuitBreakerStateType, StorageErrorCodeType } from '../types'; export { StorageErrorCode, StorageErrorCodeType } from '../types'; /** * Storage error class */ export declare class StorageError extends Error { /** * Error code */ readonly code: StorageErrorCodeType; /** * Error details */ readonly details: Record<string, any>; /** * Create a new storage error * * @param code - Error code * @param message - Error message * @param details - Error details */ constructor(code: StorageErrorCodeType, message: string, details?: Record<string, any>); /** * Convert error to string * * @returns String representation of error */ toString(): string; /** * Convert error to JSON * * @returns JSON representation of error */ toJSON(): Record<string, any>; } /** * Circuit breaker implementation * * Implements the circuit breaker pattern to prevent cascading failures * by failing fast when a service is unavailable. */ export declare class CircuitBreaker { private state; private failureCount; private lastFailureTime; private readonly failureThreshold; private readonly resetTimeoutMs; /** * Create a new circuit breaker * * @param failureThreshold - Number of failures before opening circuit * @param resetTimeoutMs - Reset timeout in milliseconds */ constructor(failureThreshold?: number, resetTimeoutMs?: number); /** * Execute a function with circuit breaker protection * * @param fn - Function to execute * @returns Function result * @throws Error if circuit is open */ execute<T>(fn: () => Promise<T>): Promise<T>; /** * Get circuit breaker state * * @returns Circuit breaker state */ getState(): CircuitBreakerStateType; /** * Reset circuit breaker */ reset(): void; } /** * Execute a function with retry logic * * @param fn - Function to execute * @param maxRetries - Maximum number of retries * @param baseDelayMs - Base delay in milliseconds * @returns Function result * @throws Error if all retries fail */ export declare function withRetry<T>(fn: () => Promise<T>, maxRetries?: number, baseDelayMs?: number): Promise<T>; /** * Execute a function with timeout * * @param promise - Promise to execute * @param timeoutMs - Timeout in milliseconds * @returns Promise result * @throws Error if timeout is reached */ export declare function withTimeout<T>(promise: Promise<T>, timeoutMs?: number): Promise<T>; /** * Execute a function with bulkhead protection * * @param fn - Function to execute * @param maxConcurrent - Maximum number of concurrent executions * @param maxQueue - Maximum queue size * @returns Function result * @throws Error if queue is full */ export declare function withBulkhead<T>(fn: () => Promise<T>, maxConcurrent?: number, maxQueue?: number): Promise<T>;