/** * @file Enterprise Error Handling Extension * @description Comprehensive structured error handling system for the enzyme library * * Implements patterns from PR #16 recommendations: * - Prisma: Structured error codes with documentation * - axios: Rich error context and recovery mechanisms * - socket.io: Error hierarchy and retry logic * - DX research: Developer-friendly messages with remediation hints * * Features: * 1. Error Code System - DOMAIN_CATEGORY_NUMBER format * 2. Error Registry - centralized error definitions * 3. Error Hierarchy - EnzymeError base class with subtypes * 4. Rich Context - detailed error metadata * 5. Remediation Hints - actionable suggestions * 6. Fuzzy Matching - Levenshtein-based typo recovery * 7. Error Serialization - JSON serialization for transport * 8. Error Recovery - retry and fallback mechanisms * 9. Error Aggregation - collect multiple errors * 10. i18n Support - localized error messages */ /** * Error severity levels */ export type ErrorSeverity = 'critical' | 'error' | 'warning' | 'info'; /** * Error domains following DOMAIN_CATEGORY_NUMBER pattern */ export type ErrorDomain = 'AUTH' | 'API' | 'STATE' | 'CONFIG' | 'VALID' | 'RENDER' | 'PERF'; /** * Error code definition */ export interface ErrorCodeDefinition { /** Full error code (e.g., "AUTH_TOKEN_001") */ code: string; /** Error domain */ domain: ErrorDomain; /** Error category within domain */ category: string; /** Human-readable error message template */ message: string; /** Error severity */ severity: ErrorSeverity; /** Whether this error is retryable */ retryable: boolean; /** Remediation hint for developers */ remediation: string; /** Documentation URL */ documentation?: string; /** Related error codes */ relatedCodes?: string[]; } /** * Error context for rich error information */ export interface ErrorContext { /** Timestamp when error occurred */ timestamp: Date; /** Component or module where error occurred */ component?: string; /** User ID if applicable */ userId?: string; /** Request ID for tracing */ requestId?: string; /** Additional metadata */ metadata?: Record; /** Stack trace */ stack?: string; /** Error location */ location?: { file?: string; line?: number; column?: number; }; } /** * Retry configuration */ export interface RetryConfig { /** Maximum number of retry attempts */ maxAttempts: number; /** Initial delay in milliseconds */ initialDelay: number; /** Maximum delay in milliseconds */ maxDelay: number; /** Backoff multiplier */ backoffMultiplier: number; /** Whether to use exponential backoff */ exponentialBackoff: boolean; } /** * Recovery strategy */ export type RecoveryStrategy = { type: 'retry'; config: RetryConfig; } | { type: 'fallback'; value: unknown; } | { type: 'ignore'; } | { type: 'throw'; }; /** * Serialized error format */ export interface SerializedError { code: string; domain: string; category: string; message: string; severity: ErrorSeverity; context: ErrorContext; remediation: string; retryable: boolean; documentation?: string; originalError?: { name: string; message: string; stack?: string; }; } /** * i18n translation function */ export type TranslationFunction = (key: string, params?: Record) => string; /** * Locale configuration */ export interface LocaleConfig { locale: string; translations: Record; } /** * Central registry of all error codes */ export declare const ERROR_CODE_REGISTRY: Record; /** * Base EnzymeError class with rich context and metadata */ export declare class EnzymeError extends Error { /** Error code (e.g., "AUTH_TOKEN_001") */ readonly code: string; /** Error domain */ readonly domain: ErrorDomain; /** Error category within domain */ readonly category: string; /** Error severity */ readonly severity: ErrorSeverity; /** Error context */ readonly context: ErrorContext; /** Remediation hint */ readonly remediation: string; /** Whether error is retryable */ readonly retryable: boolean; /** Documentation URL */ readonly documentation?: string; /** Original error if this wraps another error */ readonly originalError?: Error; /** Related error codes */ readonly relatedCodes?: string[]; constructor(codeOrDefinition: string | ErrorCodeDefinition, contextOrMessage?: Partial | string, originalError?: Error); /** * Get formatted error message with code and remediation */ toString(): string; /** * Serialize error to JSON for logging or transport */ toJSON(): SerializedError; } /** * Authentication/Authorization error */ export declare class AuthError extends EnzymeError { constructor(code: string, context?: Partial, originalError?: Error); } /** * API/Network error */ export declare class ApiError extends EnzymeError { constructor(code: string, context?: Partial, originalError?: Error); } /** * State management error */ export declare class StateError extends EnzymeError { constructor(code: string, context?: Partial, originalError?: Error); } /** * Configuration error */ export declare class ConfigError extends EnzymeError { constructor(code: string, context?: Partial, originalError?: Error); } /** * Validation error */ export declare class ValidationError extends EnzymeError { constructor(code: string, context?: Partial, originalError?: Error); } /** * Render error */ export declare class RenderError extends EnzymeError { constructor(code: string, context?: Partial, originalError?: Error); } /** * Performance error */ export declare class PerformanceError extends EnzymeError { constructor(code: string, context?: Partial, originalError?: Error); } /** * Aggregate multiple errors into a single error */ export declare class AggregateError extends EnzymeError { readonly errors: EnzymeError[]; constructor(errors: EnzymeError[], message?: string); /** * Get all error codes */ getCodes(): string[]; /** * Get errors by domain */ getByDomain(domain: ErrorDomain): EnzymeError[]; /** * Get errors by severity */ getBySeverity(severity: ErrorSeverity): EnzymeError[]; /** * Override toString to show all errors */ toString(): string; } /** * Retry an operation with exponential backoff */ export declare function retryWithBackoff(operation: () => Promise, config: RetryConfig): Promise; /** * Execute operation with recovery strategy */ export declare function executeWithRecovery(operation: () => Promise, strategy: RecoveryStrategy): Promise; /** * Type guard to check if error is an EnzymeError */ export declare function isEnzymeError(error: unknown): error is EnzymeError; /** * Type guard to check if error is retryable */ export declare function isRetryableError(error: unknown): boolean; /** * Format error for display */ export declare function formatError(error: unknown): string; /** * Serialize error for transport/logging */ export declare function serializeError(error: unknown): SerializedError | { message: string; }; /** * Create error from serialized data */ export declare function deserializeError(data: SerializedError): EnzymeError; /** * Register translations for a locale */ export declare function registerTranslations(locale: string, translationMap: Record): void; /** * Get translated error message */ export declare function getTranslatedMessage(code: string, locale: string, params?: Record): string; /** * Create localized error */ export declare function createLocalizedError(code: string, locale: string, context?: Partial, originalError?: Error): EnzymeError; /** * EnzymeExtension type (simplified version for standalone usage) */ export interface EnzymeExtension { name: string; version?: string; description?: string; client?: Record unknown>; } /** * Errors extension for enzyme framework */ export declare const errorsExtension: EnzymeExtension; export default errorsExtension;