From 2d3e0e87759248dd45bb0927779541ca81592bee Mon Sep 17 00:00:00 2001
From: Christopher Rhodes <christopher.rhodes@embl.de>
Date: Wed, 30 Aug 2023 09:40:30 +0200
Subject: [PATCH] Pass stack trace of unhandled exceptions to response body

---
 api.py            |  2 +-
 tests/test_api.py | 33 ++++++++++++++++++++-------------
 2 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/api.py b/api.py
index 1bf0f17d..ec6c99cf 100644
--- a/api.py
+++ b/api.py
@@ -5,7 +5,7 @@ from fastapi import FastAPI, HTTPException
 from model_server.session import Session
 from model_server.workflow import infer_image_to_image
 
-app = FastAPI()
+app = FastAPI(debug=True)
 session = Session()
 
 @app.on_event("startup")
diff --git a/tests/test_api.py b/tests/test_api.py
index f5869bdd..f2805eef 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -15,7 +15,7 @@ class TestApiFromAutomatedClient(unittest.TestCase):
         self.server_process = Process(
             target=uvicorn.run,
             args=('api:app', ),
-            kwargs={'host': host, 'port': port, 'log_level': 'info'},
+            kwargs={'host': host, 'port': port, 'log_level': 'debug'},
             daemon=True
         )
         self.uri = f'http://{host}:{port}/'
@@ -46,7 +46,7 @@ class TestApiFromAutomatedClient(unittest.TestCase):
         self.assertEqual(resp.status_code, 200)
         self.assertEqual(resp.content, b'{}')
 
-    def test_load_model(self):
+    def test_load_dummy_model(self):
         model_id = DummyImageToImageModel.model_id
         resp_load = requests.put(
             self.uri + f'models/load',
@@ -57,9 +57,21 @@ class TestApiFromAutomatedClient(unittest.TestCase):
         self.assertEqual(resp_list.status_code, 200)
         rj = resp_list.json()
         self.assertEqual(rj[model_id]['class'], 'DummyImageToImageModel')
+        return model_id
 
+    def test_respond_with_error_when_invalid_filepath_requested(self):
+        model_id = self.test_load_dummy_model()
+        resp = requests.put(
+            self.uri + f'i2i/infer/',
+            params={
+                'model_id': model_id,
+                'input_filename': 'not_a_real_file.name'
+            }
+        )
+        self.assertEqual(resp.status_code, 404, resp.content.decode())
 
-    def test_i2i_inference_errors_model_not_found(self):
+
+    def test_i2i_inference_errors_when_model_not_found(self):
         model_id = 'not_a_real_model'
         resp = requests.put(
             self.uri + f'i2i/infer/',
@@ -68,23 +80,18 @@ class TestApiFromAutomatedClient(unittest.TestCase):
                 'input_filename': 'not_a_real_file.name'
             }
         )
-        print(resp.content)
-        self.assertEqual(resp.status_code, 409)
+        self.assertEqual(resp.status_code, 409, resp.content.decode())
 
     def test_i2i_dummy_inference_by_api(self):
-        model = DummyImageToImageModel()
-        resp_load = requests.put(
-            self.uri + f'models/load',
-            params={'model_id': model.model_id}
-        )
-        self.assertEqual(resp_load.status_code, 200, f'Error loading {model.model_id}')
+        model_id = self.test_load_dummy_model()
         self.copy_input_file_to_server()
         resp_infer = requests.put(
             self.uri + f'i2i/infer/',
             params={
-                'model_id': model.model_id,
+                'model_id': model_id,
                 'input_filename': czifile['filename'],
                 'channel': 2,
             },
         )
-        self.assertEqual(resp_infer.status_code, 200, f'Error inferring from {model.model_id}')
\ No newline at end of file
+        self.assertEqual(resp_infer.status_code, 200, f'Error inferring from {model_id}', resp_infer.content.decode())
+
-- 
GitLab