-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_handler.py
More file actions
685 lines (572 loc) · 20.6 KB
/
error_handler.py
File metadata and controls
685 lines (572 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# ============================================================================
# ERROR_HANDLER.PY - Sistema Centralizado de Gestión de Errores
# PSIC-O-TRONIC - Manejo robusto de errores con logging y recuperación
# ============================================================================
import time
import gc
# Intentar importar json (MicroPython vs Python estándar)
try:
import ujson as json
except ImportError:
import json
# ============================================================================
# CATEGORÍAS DE ERROR
# ============================================================================
class ErrorCategory:
"""Categorías de errores para clasificación"""
NETWORK = "network" # Errores de red/WiFi
API = "api" # Errores de API (Gemini)
STORAGE = "storage" # Errores de almacenamiento
HARDWARE = "hardware" # Errores de hardware (LCD, botones)
MEMORY = "memory" # Errores de memoria
DATA = "data" # Errores de datos/JSON
GAME = "game" # Errores de lógica del juego
UNKNOWN = "unknown" # Errores desconocidos
class ErrorSeverity:
"""Severidad de errores"""
INFO = 0 # Informativo, no requiere acción
WARNING = 1 # Advertencia, puede continuar
ERROR = 2 # Error, requiere atención
CRITICAL = 3 # Crítico, requiere reinicio
# ============================================================================
# MENSAJES DE ERROR AMIGABLES
# ============================================================================
ERROR_MESSAGES = {
# Network errors
"wifi_disconnected": {
"titulo": "SIN WIFI",
"mensaje": "Conexion perdida",
"accion": "Reconecta WiFi",
"categoria": ErrorCategory.NETWORK,
"severidad": ErrorSeverity.ERROR,
"recuperable": True
},
"wifi_timeout": {
"titulo": "TIMEOUT WIFI",
"mensaje": "Red muy lenta",
"accion": "Acercate al router",
"categoria": ErrorCategory.NETWORK,
"severidad": ErrorSeverity.WARNING,
"recuperable": True
},
"dns_error": {
"titulo": "ERROR DNS",
"mensaje": "No resuelve nombre",
"accion": "Verifica internet",
"categoria": ErrorCategory.NETWORK,
"severidad": ErrorSeverity.ERROR,
"recuperable": True
},
# API errors
"api_key_invalid": {
"titulo": "API KEY MAL",
"mensaje": "Key invalida",
"accion": "Reconfigura API",
"categoria": ErrorCategory.API,
"severidad": ErrorSeverity.ERROR,
"recuperable": False
},
"api_rate_limit": {
"titulo": "LIMITE API",
"mensaje": "Demasiadas peticiones",
"accion": "Espera 1 minuto",
"categoria": ErrorCategory.API,
"severidad": ErrorSeverity.WARNING,
"recuperable": True
},
"api_server_error": {
"titulo": "ERROR SERVIDOR",
"mensaje": "Gemini no responde",
"accion": "Reintenta luego",
"categoria": ErrorCategory.API,
"severidad": ErrorSeverity.ERROR,
"recuperable": True
},
"api_bad_response": {
"titulo": "RESPUESTA MAL",
"mensaje": "JSON invalido",
"accion": "Reintentando...",
"categoria": ErrorCategory.API,
"severidad": ErrorSeverity.WARNING,
"recuperable": True
},
# Storage errors
"storage_full": {
"titulo": "MEMORIA LLENA",
"mensaje": "Sin espacio",
"accion": "Borra datos",
"categoria": ErrorCategory.STORAGE,
"severidad": ErrorSeverity.CRITICAL,
"recuperable": False
},
"storage_corrupt": {
"titulo": "DATOS CORRUPTOS",
"mensaje": "Archivo danado",
"accion": "Reset partida",
"categoria": ErrorCategory.STORAGE,
"severidad": ErrorSeverity.ERROR,
"recuperable": False
},
"storage_read_error": {
"titulo": "ERROR LECTURA",
"mensaje": "No lee archivo",
"accion": "Reinicia",
"categoria": ErrorCategory.STORAGE,
"severidad": ErrorSeverity.ERROR,
"recuperable": True
},
"storage_write_error": {
"titulo": "ERROR ESCRITURA",
"mensaje": "No guarda datos",
"accion": "Verifica espacio",
"categoria": ErrorCategory.STORAGE,
"severidad": ErrorSeverity.ERROR,
"recuperable": True
},
# Memory errors
"memory_low": {
"titulo": "POCA MEMORIA",
"mensaje": "RAM casi llena",
"accion": "Reinicia pronto",
"categoria": ErrorCategory.MEMORY,
"severidad": ErrorSeverity.WARNING,
"recuperable": True
},
"memory_critical": {
"titulo": "SIN MEMORIA",
"mensaje": "RAM agotada",
"accion": "Reinicia ahora",
"categoria": ErrorCategory.MEMORY,
"severidad": ErrorSeverity.CRITICAL,
"recuperable": False
},
# Hardware errors
"lcd_error": {
"titulo": "ERROR LCD",
"mensaje": "Pantalla falla",
"accion": "Revisa conexion",
"categoria": ErrorCategory.HARDWARE,
"severidad": ErrorSeverity.CRITICAL,
"recuperable": False
},
"button_stuck": {
"titulo": "BOTON ATASCADO",
"mensaje": "Boton pulsado",
"accion": "Suelta el boton",
"categoria": ErrorCategory.HARDWARE,
"severidad": ErrorSeverity.WARNING,
"recuperable": True
},
# Data errors
"json_parse_error": {
"titulo": "ERROR JSON",
"mensaje": "Datos mal formato",
"accion": "Reintentando...",
"categoria": ErrorCategory.DATA,
"severidad": ErrorSeverity.WARNING,
"recuperable": True
},
"missing_data": {
"titulo": "FALTAN DATOS",
"mensaje": "Campos vacios",
"accion": "Regenerando...",
"categoria": ErrorCategory.DATA,
"severidad": ErrorSeverity.WARNING,
"recuperable": True
},
# Game errors
"patient_error": {
"titulo": "ERROR PACIENTE",
"mensaje": "No genera paciente",
"accion": "Reintentando...",
"categoria": ErrorCategory.GAME,
"severidad": ErrorSeverity.WARNING,
"recuperable": True
},
"session_error": {
"titulo": "ERROR SESION",
"mensaje": "Sesion fallida",
"accion": "Volviendo...",
"categoria": ErrorCategory.GAME,
"severidad": ErrorSeverity.WARNING,
"recuperable": True
},
# Generic
"unknown_error": {
"titulo": "ERROR",
"mensaje": "Algo salio mal",
"accion": "Reintenta",
"categoria": ErrorCategory.UNKNOWN,
"severidad": ErrorSeverity.ERROR,
"recuperable": True
}
}
# ============================================================================
# CLASE PRINCIPAL DE GESTIÓN DE ERRORES
# ============================================================================
class ErrorHandler:
"""Gestor centralizado de errores"""
# Archivo de log
LOG_FILE = "/error_log.json"
MAX_LOG_ENTRIES = 50
# Singleton
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self):
if self._initialized:
return
self._initialized = True
# Estado actual
self.current_error = None
self.error_count = 0
self.retry_count = 0
self.max_retries = 3
# Historial de errores (en memoria)
self.error_history = []
self.max_history = 20
# Estadísticas por categoría
self.stats = {
ErrorCategory.NETWORK: 0,
ErrorCategory.API: 0,
ErrorCategory.STORAGE: 0,
ErrorCategory.HARDWARE: 0,
ErrorCategory.MEMORY: 0,
ErrorCategory.DATA: 0,
ErrorCategory.GAME: 0,
ErrorCategory.UNKNOWN: 0
}
# Callbacks
self._on_error_callback = None
self._on_recover_callback = None
# Cargar log existente
self._load_log()
def set_callbacks(self, on_error=None, on_recover=None):
"""
Configura callbacks para eventos de error.
Args:
on_error: Función llamada cuando ocurre un error (error_info)
on_recover: Función llamada cuando se recupera de un error
"""
self._on_error_callback = on_error
self._on_recover_callback = on_recover
def report(self, error_type, details="", exception=None):
"""
Reporta un error al sistema.
Args:
error_type: Tipo de error (clave de ERROR_MESSAGES)
details: Detalles adicionales
exception: Excepción original si existe
Returns:
Dict con información del error
"""
# Obtener información del error
error_info = ERROR_MESSAGES.get(error_type, ERROR_MESSAGES["unknown_error"]).copy()
error_info["type"] = error_type
error_info["details"] = details
error_info["timestamp"] = time.time()
error_info["retry_count"] = self.retry_count
if exception:
error_info["exception"] = f"{type(exception).__name__}: {str(exception)}"
# Actualizar estadísticas
categoria = error_info.get("categoria", ErrorCategory.UNKNOWN)
self.stats[categoria] = self.stats.get(categoria, 0) + 1
self.error_count += 1
# Guardar en historial
self._add_to_history(error_info)
# Log
self._log_error(error_info)
# Callback
if self._on_error_callback:
try:
self._on_error_callback(error_info)
except:
pass # No propagar errores del callback
# Imprimir para debug
print(f"[ERROR] {error_info['titulo']}: {error_info['mensaje']} ({details})")
# Actualizar estado
self.current_error = error_info
return error_info
def report_exception(self, exception, context=""):
"""
Reporta una excepción automáticamente clasificada.
Args:
exception: La excepción capturada
context: Contexto donde ocurrió
Returns:
Dict con información del error
"""
# Clasificar excepción
exc_type = type(exception).__name__
exc_msg = str(exception)
# Mapear tipo de excepción a tipo de error
if exc_type == "MemoryError":
error_type = "memory_critical"
elif exc_type == "OSError":
if "ECONNRESET" in exc_msg or "ETIMEDOUT" in exc_msg:
error_type = "wifi_timeout"
elif "ENOENT" in exc_msg:
error_type = "storage_read_error"
elif "ENOSPC" in exc_msg:
error_type = "storage_full"
else:
error_type = "wifi_disconnected"
elif exc_type == "ValueError":
error_type = "json_parse_error"
elif exc_type == "KeyError":
error_type = "missing_data"
else:
error_type = "unknown_error"
return self.report(error_type, f"{context}: {exc_msg}", exception)
def clear(self):
"""Limpia el error actual (recuperación exitosa)"""
if self.current_error and self._on_recover_callback:
try:
self._on_recover_callback()
except:
pass
self.current_error = None
self.retry_count = 0
def can_retry(self):
"""
Comprueba si se puede reintentar la operación.
Returns:
True si quedan reintentos
"""
if not self.current_error:
return True
if not self.current_error.get("recuperable", False):
return False
return self.retry_count < self.max_retries
def increment_retry(self):
"""Incrementa contador de reintentos"""
self.retry_count += 1
return self.retry_count
def get_retry_delay(self):
"""
Obtiene delay recomendado para reintento (backoff exponencial).
Returns:
Segundos a esperar
"""
# Backoff: 1s, 2s, 4s, 8s...
return min(2 ** self.retry_count, 30)
def get_display_info(self):
"""
Obtiene información formateada para mostrar en LCD.
Returns:
Dict con lineas para LCD 20x4
"""
if not self.current_error:
return None
e = self.current_error
return {
"line0": e.get("titulo", "ERROR")[:20].center(20),
"line1": e.get("mensaje", "")[:20].center(20),
"line2": e.get("accion", "")[:20].center(20),
"line3": "[OK] Reintentar" if e.get("recuperable") else "[OK] Volver"
}
def get_stats(self):
"""
Obtiene estadísticas de errores.
Returns:
Dict con estadísticas
"""
return {
"total": self.error_count,
"por_categoria": self.stats.copy(),
"historial_reciente": len(self.error_history)
}
def get_history(self, limit=10):
"""
Obtiene historial de errores recientes.
Args:
limit: Número máximo de entradas
Returns:
Lista de errores recientes
"""
return self.error_history[-limit:]
def check_memory(self):
"""
Verifica estado de memoria y reporta si es bajo.
Returns:
Tuple (free_bytes, is_critical)
"""
gc.collect()
try:
free = gc.mem_free()
total = gc.mem_alloc() + free
pct_free = (free * 100) // total
if pct_free < 10:
self.report("memory_critical", f"{free} bytes libres")
return (free, True)
elif pct_free < 20:
self.report("memory_low", f"{free} bytes libres")
return (free, False)
return (free, False)
except:
return (0, False)
def _add_to_history(self, error_info):
"""Añade error al historial en memoria"""
# Copiar solo campos esenciales para ahorrar memoria
entry = {
"type": error_info.get("type"),
"titulo": error_info.get("titulo"),
"timestamp": error_info.get("timestamp"),
"categoria": error_info.get("categoria")
}
self.error_history.append(entry)
# Limitar tamaño
while len(self.error_history) > self.max_history:
self.error_history.pop(0)
def _log_error(self, error_info):
"""Guarda error en archivo de log"""
try:
# Leer log existente
log_data = self._load_log_file()
# Añadir nuevo error
entry = {
"t": error_info.get("type"),
"ts": int(error_info.get("timestamp", 0)),
"d": error_info.get("details", "")[:50] # Limitar detalles
}
log_data["errors"].append(entry)
# Limitar tamaño del log
while len(log_data["errors"]) > self.MAX_LOG_ENTRIES:
log_data["errors"].pop(0)
# Actualizar contadores
log_data["total"] = log_data.get("total", 0) + 1
# Guardar
self._save_log_file(log_data)
except Exception as e:
print(f"[ERROR_HANDLER] No pudo guardar log: {e}")
def _load_log_file(self):
"""Carga archivo de log"""
try:
with open(self.LOG_FILE, 'r') as f:
return json.load(f)
except:
return {"errors": [], "total": 0}
def _save_log_file(self, data):
"""Guarda archivo de log"""
try:
with open(self.LOG_FILE, 'w') as f:
json.dump(data, f)
except:
pass
def _load_log(self):
"""Carga estadísticas del log al iniciar"""
try:
log_data = self._load_log_file()
# Restaurar contador total
self.error_count = log_data.get("total", 0)
except:
pass
def clear_log(self):
"""Borra el log de errores"""
try:
with open(self.LOG_FILE, 'w') as f:
json.dump({"errors": [], "total": 0}, f)
self.error_count = 0
self.error_history = []
self.stats = {k: 0 for k in self.stats}
print("[ERROR_HANDLER] Log borrado")
except Exception as e:
print(f"[ERROR_HANDLER] Error borrando log: {e}")
# ============================================================================
# DECORADOR PARA MANEJO AUTOMÁTICO DE ERRORES
# ============================================================================
def handle_errors(error_type="unknown_error", context="", retries=3):
"""
Decorador para manejar errores automáticamente.
Args:
error_type: Tipo de error por defecto
context: Contexto de la operación
retries: Número de reintentos
"""
def decorator(func):
def wrapper(*args, **kwargs):
handler = ErrorHandler()
for attempt in range(retries):
try:
result = func(*args, **kwargs)
handler.clear() # Éxito, limpiar error
return result
except Exception as e:
handler.report_exception(e, context or func.__name__)
if attempt < retries - 1 and handler.can_retry():
handler.increment_retry()
delay = handler.get_retry_delay()
print(f"[RETRY] Intento {attempt + 2}/{retries} en {delay}s...")
time.sleep(delay)
else:
# Último intento fallido
return None
return None
return wrapper
return decorator
# ============================================================================
# FUNCIONES DE UTILIDAD
# ============================================================================
def get_error_handler():
"""Obtiene la instancia singleton del manejador de errores"""
return ErrorHandler()
def report_error(error_type, details="", exception=None):
"""Atajo para reportar un error"""
return ErrorHandler().report(error_type, details, exception)
def check_memory():
"""Atajo para verificar memoria"""
return ErrorHandler().check_memory()
# ============================================================================
# HTTP ERROR MAPPER
# ============================================================================
def map_http_error(status_code):
"""
Mapea código HTTP a tipo de error.
Args:
status_code: Código HTTP
Returns:
Tipo de error
"""
if status_code == 400:
return "api_key_invalid"
elif status_code == 401:
return "api_key_invalid"
elif status_code == 403:
return "api_key_invalid"
elif status_code == 429:
return "api_rate_limit"
elif status_code >= 500:
return "api_server_error"
else:
return "unknown_error"
# ============================================================================
# TEST
# ============================================================================
if __name__ == "__main__":
# Test básico
handler = ErrorHandler()
print("=== Test Error Handler ===")
# Reportar errores
handler.report("wifi_disconnected", "Test de desconexión")
handler.report("api_rate_limit", "Demasiadas peticiones")
# Simular excepción
try:
raise ValueError("Test exception")
except Exception as e:
handler.report_exception(e, "test_function")
# Mostrar estadísticas
stats = handler.get_stats()
print(f"\nEstadísticas: {stats}")
# Mostrar historial
history = handler.get_history()
print(f"\nHistorial: {history}")
# Info para LCD
info = handler.get_display_info()
if info:
print(f"\nLCD:")
for k, v in info.items():
print(f" {k}: '{v}'")
print("\n=== Test OK ===")