UNPKG

@sailboat-computer/event-bus

Version:

Standardized event bus for sailboat computer v3 with resilience features and offline capabilities

220 lines (188 loc) 4.74 kB
/** * Event bus error types */ /** * Base error class for event bus errors */ export class EventBusError extends Error { /** * Error code */ public readonly code: EventBusErrorCode; /** * Error details */ public readonly details: Record<string, any> | undefined; /** * Create a new event bus error * * @param code - Error code * @param message - Error message * @param details - Error details */ constructor(code: EventBusErrorCode, message: string, details?: Record<string, any>) { super(message); this.name = 'EventBusError'; this.code = code; this.details = details || undefined; // Ensure proper prototype chain for instanceof checks Object.setPrototypeOf(this, EventBusError.prototype); } } /** * Error codes for event bus errors */ export enum EventBusErrorCode { /** * Initialization error */ INITIALIZATION_FAILED = 'INITIALIZATION_FAILED', /** * Connection error */ CONNECTION_FAILED = 'CONNECTION_FAILED', /** * Publish error */ PUBLISH_FAILED = 'PUBLISH_FAILED', /** * Subscribe error */ SUBSCRIBE_FAILED = 'SUBSCRIBE_FAILED', /** * Acknowledgment error */ ACKNOWLEDGE_FAILED = 'ACKNOWLEDGE_FAILED', /** * Configuration error */ INVALID_CONFIGURATION = 'INVALID_CONFIGURATION', /** * Adapter error */ ADAPTER_ERROR = 'ADAPTER_ERROR', /** * Timeout error */ TIMEOUT = 'TIMEOUT', /** * Not initialized error */ NOT_INITIALIZED = 'NOT_INITIALIZED', /** * Already initialized error */ ALREADY_INITIALIZED = 'ALREADY_INITIALIZED', /** * Already closed error */ ALREADY_CLOSED = 'ALREADY_CLOSED', /** * Invalid event type error */ INVALID_EVENT_TYPE = 'INVALID_EVENT_TYPE', /** * Invalid event data error */ INVALID_EVENT_DATA = 'INVALID_EVENT_DATA', /** * Invalid subscription error */ INVALID_SUBSCRIPTION = 'INVALID_SUBSCRIPTION', /** * Buffer full error */ BUFFER_FULL = 'BUFFER_FULL', /** * Event not found error */ EVENT_NOT_FOUND = 'EVENT_NOT_FOUND', /** * Unknown error */ UNKNOWN_ERROR = 'UNKNOWN_ERROR' } /** * Connection error class */ export class ConnectionError extends EventBusError { /** * Create a new connection error * * @param message - Error message * @param details - Error details */ constructor(message: string, details?: Record<string, any>) { super(EventBusErrorCode.CONNECTION_FAILED, message, details); this.name = 'ConnectionError'; // Ensure proper prototype chain for instanceof checks Object.setPrototypeOf(this, ConnectionError.prototype); } } /** * Publish error class */ export class PublishError extends EventBusError { /** * Create a new publish error * * @param message - Error message * @param details - Error details */ constructor(message: string, details?: Record<string, any>) { super(EventBusErrorCode.PUBLISH_FAILED, message, details); this.name = 'PublishError'; // Ensure proper prototype chain for instanceof checks Object.setPrototypeOf(this, PublishError.prototype); } } /** * Subscribe error class */ export class SubscribeError extends EventBusError { /** * Create a new subscribe error * * @param message - Error message * @param details - Error details */ constructor(message: string, details?: Record<string, any>) { super(EventBusErrorCode.SUBSCRIBE_FAILED, message, details); this.name = 'SubscribeError'; // Ensure proper prototype chain for instanceof checks Object.setPrototypeOf(this, SubscribeError.prototype); } } /** * Configuration error class */ export class ConfigurationError extends EventBusError { /** * Create a new configuration error * * @param message - Error message * @param details - Error details */ constructor(message: string, details?: Record<string, any>) { super(EventBusErrorCode.INVALID_CONFIGURATION, message, details); this.name = 'ConfigurationError'; // Ensure proper prototype chain for instanceof checks Object.setPrototypeOf(this, ConfigurationError.prototype); } } /** * Not initialized error class */ export class NotInitializedError extends EventBusError { /** * Create a new not initialized error * * @param message - Error message */ constructor(message: string = 'Event bus not initialized') { super(EventBusErrorCode.NOT_INITIALIZED, message); this.name = 'NotInitializedError'; // Ensure proper prototype chain for instanceof checks Object.setPrototypeOf(this, NotInitializedError.prototype); } }