Просмотр исходного кода

fix: handle SQLAlchemy errors and streamline empty result response in find_blacklisted_sites function

Manuel 8 месяцев назад
Родитель
Сommit
efdacfe74d
1 измененных файлов с 28 добавлено и 16 удалено
  1. 28 16
      services/analytics/blacklist_users.py

+ 28 - 16
services/analytics/blacklist_users.py

@@ -169,22 +169,34 @@ def find_blacklisted_sites(
     global _cache_data, _cache_time
 
     # Early-exit: no active blacklist entries at all.
-    if (
-        not db.query(BlacklistDomain)
-        .filter(BlacklistDomain.active == 1)
-        .limit(1)
-        .first()
-    ):
-        return {
-            "results": [],
-            "pagination": {
-                "total": 0,
-                "total_requests": 0,
-                "page": page,
-                "per_page": per_page,
-                "total_pages": 0,
-            },
-        }
+    _EMPTY_RESULT = {
+        "results": [],
+        "domain_capped": False,
+        "domain_cap": _BLACKLIST_DOMAIN_CAP,
+        "pagination": {
+            "total": 0,
+            "total_requests": 0,
+            "page": page,
+            "per_page": per_page,
+            "total_pages": 0,
+        },
+    }
+
+    try:
+        has_active = (
+            db.query(BlacklistDomain)
+            .filter(BlacklistDomain.active == 1)
+            .limit(1)
+            .first()
+        )
+    except SQLAlchemyError:
+        logger.warning(
+            "blacklist_domains table is not available — returning empty results"
+        )
+        return _EMPTY_RESULT
+
+    if not has_active:
+        return _EMPTY_RESULT
 
     # --- Cache read ---
     now = time.monotonic()