Browse Source

fix: add error handling for backup listing and improve user feedback in the admin interface

Manuel 8 months ago
parent
commit
948bc8cc0f
3 changed files with 34 additions and 18 deletions
  1. 5 2
      routes/admin/backup.py
  2. 26 16
      services/database/backup_service.py
  3. 3 0
      templates/admin/backup.html

+ 5 - 2
routes/admin/backup.py

@@ -51,8 +51,11 @@ def register_routes(bp):
     @bp.route("/backup/list")
     @admin_required
     def backup_list():
-        backups = backup_service.list_backups()
-        return jsonify({"status": "success", "backups": backups})
+        try:
+            backups = backup_service.list_backups()
+            return jsonify({"status": "success", "backups": backups})
+        except Exception as e:
+            return jsonify({"status": "error", "message": str(e) or 'Error leyendo salvas'}), 500
 
     @bp.route("/backup/download/<filename>")
     @admin_required

+ 26 - 16
services/database/backup_service.py

@@ -130,22 +130,32 @@ def list_backups(cfg: dict | None = None) -> list[dict]:
     """Return a list of backup metadata dicts, newest first."""
     if cfg is None:
         cfg = load_config()
-    bdir = _backup_dir(cfg)
-    result = []
-    for f in _list_backup_files(bdir):
-        stat = f.stat()
-        created = datetime.fromtimestamp(stat.st_mtime)
-        btype = "auto" if "_auto_" in f.name else "manual"
-        result.append(
-            {
-                "filename": f.name,
-                "created_at": created.strftime("%Y-%m-%d %H:%M"),
-                "size_bytes": stat.st_size,
-                "size_human": _human_size(stat.st_size),
-                "type": btype,
-            }
-        )
-    return result
+
+    try:
+        bdir = _backup_dir(cfg)
+    except Exception as e:
+        logger.error(f"No se puede acceder al directorio de salvas: {e}")
+        return []
+
+    try:
+        result = []
+        for f in _list_backup_files(bdir):
+            stat = f.stat()
+            created = datetime.fromtimestamp(stat.st_mtime)
+            btype = "auto" if "_auto_" in f.name else "manual"
+            result.append(
+                {
+                    "filename": f.name,
+                    "created_at": created.strftime("%Y-%m-%d %H:%M"),
+                    "size_bytes": stat.st_size,
+                    "size_human": _human_size(stat.st_size),
+                    "type": btype,
+                }
+            )
+        return result
+    except Exception as e:
+        logger.exception(f"Error listando salvas: {e}")
+        return []
 
 
 # Retention enforcement

+ 3 - 0
templates/admin/backup.html

@@ -183,6 +183,9 @@
                 <button @click="loadBackups()" class="text-xs text-blue-600 hover:text-blue-800 font-medium flex items-center gap-1 transition-colors">
                     <i class="fas fa-sync-alt" :class="loadingList ? 'animate-spin' : ''"></i> Actualizar
                 </button>
+                <button @click="statusMsg=''" class="text-xs text-gray-500 hover:text-gray-700 font-medium px-2">
+                    <i class="fas fa-times"></i> Cerrar mensaje
+                </button>
             </div>
 
             <!-- Loading -->