UNPKG

@dorothywebb/any-browser-mcp

Version:

Any Browser MCP - Launch Chrome with your actual data in debug mode for comprehensive browser automation

222 lines 6.93 kB
/** * Error Handler Utility for Any Browser MCP * Provides centralized error handling, logging, and reporting */ import { MCPError, ErrorCode } from '../types/errors.js'; export class ErrorHandler { options; errorHistory = []; static instance; constructor(options = {}) { this.options = { verbose: false, logToConsole: true, includeStack: false, maxErrorHistory: 100, ...options }; } static getInstance(options) { if (!ErrorHandler.instance) { ErrorHandler.instance = new ErrorHandler(options); } return ErrorHandler.instance; } /** * Handle an error with appropriate logging and reporting */ handleError(error, context) { const mcpError = this.ensureMCPError(error); const report = { error: mcpError, timestamp: new Date(), handled: true, context }; // Add to history this.addToHistory(report); // Log the error if (this.options.logToConsole) { this.logError(mcpError); } return report; } /** * Convert any error to MCPError if needed */ ensureMCPError(error) { if (error instanceof MCPError) { return error; } // Convert standard errors to MCPError return new (class extends MCPError { constructor(originalError) { super(originalError.message, ErrorCode.SYSTEM_ERROR, { component: 'ErrorHandler', details: { originalErrorType: originalError.constructor.name } }, [{ action: 'Check Logs', description: 'Review error details and stack trace for more information' }]); this.stack = originalError.stack; } })(error); } /** * Log error to console with formatting */ logError(error) { const timestamp = new Date().toISOString(); if (this.options.verbose) { console.error(`\n🚨 [${timestamp}] MCP Error Occurred:`); console.error(error.getFormattedMessage()); if (this.options.includeStack && error.stack) { console.error('\nStack Trace:'); console.error(error.stack); } } else { console.error(`🚨 [${error.code}] ${error.message}`); if (error.recoveryActions.length > 0) { console.error('💡 Suggested actions:'); error.recoveryActions.slice(0, 2).forEach((action, index) => { console.error(` ${index + 1}. ${action.description}`); }); } } } /** * Add error to history with size management */ addToHistory(report) { this.errorHistory.unshift(report); if (this.errorHistory.length > this.options.maxErrorHistory) { this.errorHistory = this.errorHistory.slice(0, this.options.maxErrorHistory); } } /** * Get recent error history */ getErrorHistory(limit) { return limit ? this.errorHistory.slice(0, limit) : [...this.errorHistory]; } /** * Get error statistics */ getErrorStats() { const stats = { total: this.errorHistory.length, byCode: {}, byComponent: {}, recent: 0 }; const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000); this.errorHistory.forEach(report => { // Count by error code const code = report.error.code; stats.byCode[code] = (stats.byCode[code] || 0) + 1; // Count by component const component = report.error.context.component || 'Unknown'; stats.byComponent[component] = (stats.byComponent[component] || 0) + 1; // Count recent errors if (report.timestamp > oneHourAgo) { stats.recent++; } }); return stats; } /** * Clear error history */ clearHistory() { this.errorHistory = []; } /** * Check if system is experiencing high error rates */ isHighErrorRate() { const recentErrors = this.getErrorStats().recent; return recentErrors > 10; // More than 10 errors in the last hour } /** * Get health status based on error patterns */ getHealthStatus() { const stats = this.getErrorStats(); if (stats.recent === 0) { return { status: 'healthy', message: 'No recent errors detected', recommendations: [] }; } if (stats.recent < 5) { return { status: 'warning', message: `${stats.recent} errors in the last hour`, recommendations: [ 'Monitor error patterns', 'Check browser connectivity', 'Verify configuration' ] }; } return { status: 'critical', message: `High error rate: ${stats.recent} errors in the last hour`, recommendations: [ 'Restart MCP server', 'Check browser status', 'Verify system resources', 'Review configuration', 'Check network connectivity' ] }; } /** * Export error data for debugging */ exportErrorData() { return { timestamp: new Date().toISOString(), stats: this.getErrorStats(), recentErrors: this.getErrorHistory(10).map(report => ({ code: report.error.code, message: report.error.message, component: report.error.context.component, timestamp: report.timestamp, recoveryActions: report.error.recoveryActions.length })), healthStatus: this.getHealthStatus() }; } /** * Set error handler options */ setOptions(options) { this.options = { ...this.options, ...options }; } } /** * Global error handler instance */ export const globalErrorHandler = ErrorHandler.getInstance(); /** * Convenience function for handling errors */ export function handleError(error, context) { return globalErrorHandler.handleError(error, context); } /** * Convenience function for getting error stats */ export function getErrorStats() { return globalErrorHandler.getErrorStats(); } /** * Convenience function for getting health status */ export function getHealthStatus() { return globalErrorHandler.getHealthStatus(); } //# sourceMappingURL=ErrorHandler.js.map