From 4076fc5cb10dac24f563fc44fa2437e866f64d40 Mon Sep 17 00:00:00 2001
From: schroeder5 <s.schroeder@fz-juelich.de>
Date: Wed, 12 Feb 2025 16:45:54 +0000
Subject: [PATCH] avoid an internal server error when an unknown request id is
 passed to the endpoint 'request_timeseries_list_of_contributors'

---
 tests/test_timeseries.py  |  8 ++++++++
 toardb/timeseries/crud.py | 11 ++++++++---
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/tests/test_timeseries.py b/tests/test_timeseries.py
index c30e6b4..b2711a8 100644
--- a/tests/test_timeseries.py
+++ b/tests/test_timeseries.py
@@ -1084,6 +1084,14 @@ class TestApps:
         assert response.json() == expected_response
 
 
+    def test_request_registered_contributors_list_unknown_rid(self, client, db):
+        response = client.get("/timeseries/request_timeseries_list_of_contributors/7f0df73a-bd0f-58b9-bb17-d5cd36f89598?format=text")
+        expected_status_code = 400
+        assert response.status_code == expected_status_code
+        expected_response = 'not a registered request id: 7f0df73a-bd0f-58b9-bb17-d5cd36f89598'
+        assert response.json() == expected_response
+
+
     # 3. tests updating timeseries metadata
 
     def test_patch_timeseries_no_description(self, client, db):
diff --git a/toardb/timeseries/crud.py b/toardb/timeseries/crud.py
index a2e7332..596b96a 100644
--- a/toardb/timeseries/crud.py
+++ b/toardb/timeseries/crud.py
@@ -618,9 +618,14 @@ def get_request_contributors(db: Session, format: str = 'text', input_handle: Up
 
 
 def get_registered_request_contributors(db: Session, rid, format: str = 'text'):
-    timeseries_ids = db.execute(select([s1_contributors_table]).\
-                                where(s1_contributors_table.c.request_id == rid)).mappings().first()['timeseries_ids']
-    return get_contributors_list(db, timeseries_ids, format)
+    try:
+        timeseries_ids = db.execute(select([s1_contributors_table]).\
+                                    where(s1_contributors_table.c.request_id == rid)).mappings().first()['timeseries_ids']
+        return get_contributors_list(db, timeseries_ids, format)
+    except:
+        status_code=400
+        message=f"not a registered request id: {rid}"
+        return JSONResponse(status_code=status_code, content=message)
 
 
 def register_request_contributors(db: Session, rid, ids):
-- 
GitLab