@tachui/devtools
Version:
Development & debugging tools for tachUI framework
302 lines • 7.9 kB
TypeScript
/**
* Error Boundary and Error Handling System (Phase 3.2.3)
*
* Comprehensive error handling system for TachUI applications.
* Provides error boundaries, recovery mechanisms, and error reporting.
*/
import type { ComponentInstance, ComponentProps, DOMNode } from '@tachui/core';
/**
* Error severity levels
*/
export type ErrorSeverity = 'low' | 'medium' | 'high' | 'critical';
/**
* Error categories for classification
*/
export type ErrorCategory = 'component_error' | 'reactive_error' | 'render_error' | 'lifecycle_error' | 'network_error' | 'validation_error' | 'unknown_error';
/**
* Comprehensive error information
*/
export interface TachUIError {
id: string;
message: string;
stack?: string;
cause?: Error;
timestamp: number;
category: ErrorCategory;
severity: ErrorSeverity;
componentId?: string;
componentName?: string;
phase?: string;
context?: Record<string, any>;
userAgent?: string;
url?: string;
retryCount: number;
recovered: boolean;
}
/**
* Error boundary state
*/
export interface ErrorBoundaryState {
hasError: boolean;
error: TachUIError | null;
errorInfo: {
componentStack?: string;
errorBoundary?: string;
retryAttempts: number;
};
}
/**
* Error recovery strategies
*/
export type ErrorRecoveryStrategy = 'retry' | 'fallback' | 'reload' | 'redirect' | 'ignore' | 'escalate';
/**
* Error recovery configuration
*/
export interface ErrorRecoveryConfig {
strategy: ErrorRecoveryStrategy;
maxRetries?: number;
retryDelay?: number;
fallbackComponent?: ComponentInstance;
onRecovery?: (error: TachUIError) => void;
condition?: (error: TachUIError) => boolean;
}
/**
* Error boundary props
*/
export interface ErrorBoundaryProps extends ComponentProps {
fallback?: ComponentInstance | ((error: TachUIError, retry: () => void) => ComponentInstance);
onError?: (error: TachUIError, errorInfo: ErrorBoundaryState['errorInfo']) => void;
recovery?: ErrorRecoveryConfig[];
isolate?: boolean;
resetOnPropsChange?: boolean;
resetKeys?: string[];
}
/**
* Error reporter interface
*/
export interface ErrorReporter {
report(error: TachUIError): void | Promise<void>;
}
/**
* Error handler function type
*/
export type ErrorHandler = (error: TachUIError) => void | Promise<void>;
/**
* Global error handling configuration
*/
export interface ErrorHandlingConfig {
enabled: boolean;
captureConsoleErrors: boolean;
captureUnhandledPromises: boolean;
captureReactiveErrors: boolean;
captureComponentErrors: boolean;
maxErrorsPerSession: number;
maxErrorAge: number;
reportingThrottle: number;
enableStackTrace: boolean;
enableSourceMap: boolean;
development: boolean;
}
/**
* Error boundary component implementation
*/
export declare class ErrorBoundary {
private state;
private props;
private children;
private errorManager;
private stateSignal;
private setState;
constructor(props: ErrorBoundaryProps, children: ComponentInstance[], errorManager: ErrorManager);
/**
* Catch and handle errors in child components
*/
componentDidCatch(error: Error, errorInfo: any): void;
/**
* Handle error and determine recovery strategy
*/
private handleError;
/**
* Attempt error recovery based on configured strategies
*/
private attemptRecovery;
/**
* Retry failed operation with configuration
*/
private retryWithConfig;
/**
* Retry the failed operation
*/
retry(): void;
/**
* Reset error boundary to normal state
*/
resetErrorBoundary(): void;
/**
* Reload the component
*/
private reloadComponent;
/**
* Setup props watcher for error boundary reset
*/
private setupPropsWatcher;
/**
* Check if error boundary should reset based on props
*/
private shouldResetOnProps;
/**
* Render error boundary content
*/
render(): DOMNode[];
/**
* Render default error UI
*/
private renderDefaultErrorUI;
/**
* Get current error boundary state
*/
getState(): ErrorBoundaryState;
/**
* Get reactive state signal
*/
getStateSignal(): () => ErrorBoundaryState;
}
/**
* Global error manager
*/
export declare class ErrorManager {
private static instance;
private config;
private errors;
private reporters;
private handlers;
private throttleMap;
private errorsSignal;
private setErrors;
constructor();
static getInstance(): ErrorManager;
/**
* Configure error handling
*/
configure(config: Partial<ErrorHandlingConfig>): void;
/**
* Setup global error handlers
*/
private setupGlobalErrorHandlers;
/**
* Create standardized TachUI error
*/
createTachUIError(error: Error, options?: {
category?: ErrorCategory;
severity?: ErrorSeverity;
componentId?: string;
componentName?: string;
phase?: string;
context?: Record<string, any>;
}): TachUIError;
/**
* Report error to all registered reporters
*/
reportError(error: TachUIError): void;
/**
* Clean old errors based on max age
*/
private cleanOldErrors;
/**
* Add error reporter
*/
addReporter(reporter: ErrorReporter): () => void;
/**
* Add error handler
*/
addHandler(handler: ErrorHandler): () => void;
/**
* Get all errors
*/
getErrors(): TachUIError[];
/**
* Get errors signal
*/
getErrorsSignal(): () => TachUIError[];
/**
* Get errors by category
*/
getErrorsByCategory(category: ErrorCategory): TachUIError[];
/**
* Get errors by severity
*/
getErrorsBySeverity(severity: ErrorSeverity): TachUIError[];
/**
* Get errors by component
*/
getErrorsByComponent(componentId: string): TachUIError[];
/**
* Mark error as recovered
*/
markErrorRecovered(errorId: string): void;
/**
* Clear all errors
*/
clear(): void;
/**
* Get error statistics
*/
getStatistics(): {
totalErrors: number;
errorsByCategory: Record<ErrorCategory, number>;
errorsBySeverity: Record<ErrorSeverity, number>;
recoveredErrors: number;
recentErrors: number;
};
}
/**
* Global error manager instance
*/
export declare const globalErrorManager: ErrorManager;
/**
* Create error boundary component
*/
export declare function createErrorBoundary(props: ErrorBoundaryProps, children: ComponentInstance[]): ErrorBoundary;
/**
* Built-in error reporters
*/
export declare const errorReporters: {
/**
* Console reporter
*/
console: ErrorReporter;
/**
* Local storage reporter
*/
localStorage: ErrorReporter;
/**
* Remote API reporter
*/
createRemoteReporter(endpoint: string, apiKey?: string): ErrorReporter;
};
/**
* Error handling utilities
*/
export declare const errorUtils: {
/**
* Wrap function with error handling
*/
withErrorHandling<T extends (...args: any[]) => any>(fn: T, options?: {
category?: ErrorCategory;
onError?: (error: TachUIError) => void;
fallback?: ReturnType<T>;
}): T;
/**
* Wrap async function with error handling
*/
withAsyncErrorHandling<T extends (...args: any[]) => Promise<any>>(fn: T, options?: {
category?: ErrorCategory;
onError?: (error: TachUIError) => void;
fallback?: Awaited<ReturnType<T>>;
}): T;
/**
* Create error boundary with common configurations
*/
createSimpleErrorBoundary(fallbackMessage?: string): (children: ComponentInstance[]) => ErrorBoundary;
};
//# sourceMappingURL=error-boundary.d.ts.map