UNPKG

advanced-games-library

Version:

Advanced Gaming Library for React Native - Four Complete Games with iOS Compatibility Fixes

119 lines (101 loc) â€ĸ 3.01 kB
// Error handling utility export const ErrorCategory = { INITIALIZATION: 'initialization', RENDERING: 'rendering', USER_INPUT: 'user_input', NETWORK: 'network', GAME_LOGIC: 'game_logic' }; export const ErrorSeverity = { LOW: 'low', MEDIUM: 'medium', HIGH: 'high', CRITICAL: 'critical' }; class GameErrorHandler { constructor() { this.errorListeners = []; this.errorHistory = []; this.productionMode = false; } async handleError(error, category, severity, context = {}) { const errorInfo = { error: error instanceof Error ? { message: error.message, stack: error.stack, name: error.name } : error, category, severity, context, timestamp: new Date().toISOString(), id: this.generateErrorId() }; this.errorHistory.push(errorInfo); // Log to console based on severity if (severity === ErrorSeverity.CRITICAL) { console.error('🚨 CRITICAL ERROR:', errorInfo); } else if (severity === ErrorSeverity.HIGH) { console.error('âš ī¸ HIGH SEVERITY ERROR:', errorInfo); } else if (severity === ErrorSeverity.MEDIUM) { console.warn('âš ī¸ MEDIUM SEVERITY ERROR:', errorInfo); } else { console.log('â„šī¸ LOW SEVERITY ERROR:', errorInfo); } // Notify listeners this.errorListeners.forEach(listener => { try { listener(errorInfo); } catch (listenerError) { console.error('Error in error listener:', listenerError); } }); // In production mode, you might want to send to crash reporting service if (this.productionMode && severity === ErrorSeverity.CRITICAL) { await this.reportToCrashService(errorInfo); } return errorInfo; } onError(callback) { this.errorListeners.push(callback); // Return unsubscribe function return () => { const index = this.errorListeners.indexOf(callback); if (index > -1) { this.errorListeners.splice(index, 1); } }; } enableProductionMode() { this.productionMode = true; console.log('🔧 Error handler enabled in production mode'); } getErrorHistory() { return [...this.errorHistory]; } clearErrorHistory() { this.errorHistory = []; } generateErrorId() { return `err_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; } async reportToCrashService(errorInfo) { // In a real implementation, this would send to a crash reporting service console.log('📡 Reporting to crash service:', errorInfo.id); } } export const gameErrorHandler = new GameErrorHandler(); // Safe async wrapper export async function safeAsync(asyncFn, fallback, errorOptions = {}) { try { return await asyncFn(); } catch (error) { await gameErrorHandler.handleError( error, errorOptions.category || ErrorCategory.GAME_LOGIC, errorOptions.severity || ErrorSeverity.MEDIUM, errorOptions.context || {} ); return fallback; } }