From 096ad00b3222d49084c347fe0b8e7928345036fc Mon Sep 17 00:00:00 2001
From: Pelle Koster <pelle.koster@geant.org>
Date: Tue, 16 Apr 2024 07:56:47 +0200
Subject: [PATCH] better error handling

---
 inventory_provider/tasks/worker.py | 22 +++++++++++-----------
 test/conftest.py                   |  2 +-
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py
index bcf6d4f6..e92980b6 100644
--- a/inventory_provider/tasks/worker.py
+++ b/inventory_provider/tasks/worker.py
@@ -1886,18 +1886,18 @@ def populate_error_report_interfaces_cache(warning_callback=lambda s: None):
     if not all_populated_interfaces:
         previous_r = get_current_redis(InventoryTask.config)
 
-        def _load_previous(key):
-            try:
-                warning_callback(f"populating {key} "
-                                 "from previously cached data")
-                return json.loads(previous_r.get(key))
-            except Exception as e:
-                warning_callback(f"Failed to load {key} "
-                                 f"from previously cached data: {e}")
+        try:
+            warning_callback(f"populating {all_cache_key} from previously cached data")
+            previous = json.loads(previous_r.get(all_cache_key))
+            all_populated_interfaces = sorted(
+               previous, key=lambda i: (i["router"], i["name"])
+            )
+        except Exception as e:
+            warning_callback(
+                f"Failed to load {all_cache_key} from previously cached data: {e}"
+            )
+            return
 
-        all_populated_interfaces = sorted(
-            _load_previous(all_cache_key), key=lambda i: (i["router"], i["name"])
-        )
 
     router_interfaces = {}
     for ifc in all_populated_interfaces:
diff --git a/test/conftest.py b/test/conftest.py
index d726efd1..c77fc91d 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -259,7 +259,7 @@ def client(flask_config_filename, data_config_filename, mocked_redis):
     os.environ['FLASK_SETTINGS_FILENAME'] = flask_config_filename
     os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME'] = data_config_filename
     app = inventory_provider.create_app(setup_logging=False)
-    app.testing = True
+    app.testing = True  # Show exceptions instead of generid 500 status
     with app.test_client() as c:
         yield c
 
-- 
GitLab