Skip to content

Commit 3625858

Browse files
committed
handle module import failures as inference 503
1 parent cce8ca4 commit 3625858

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/api/deps/inference.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ def get_inference_service_dep() -> InferenceService:
4949
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
5050
detail=f"Inference service unavailable: {exc}",
5151
) from exc
52+
except (ModuleNotFoundError, ImportError, OSError) as exc:
53+
raise HTTPException(
54+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
55+
detail=f"Inference service unavailable: {exc}",
56+
) from exc

tests/test_api_inference_dep.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
import unittest
5+
from pathlib import Path
6+
from unittest.mock import patch
7+
8+
from fastapi import HTTPException
9+
10+
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
11+
12+
from api.deps.inference import get_inference_service_dep
13+
14+
15+
class TestApiInferenceDep(unittest.TestCase):
16+
@patch("api.deps.inference._build_inference_service", side_effect=ModuleNotFoundError("hf_xet"))
17+
@patch("api.deps.inference.resolve_artifact_uri", side_effect=lambda value: value)
18+
def test_maps_module_not_found_to_http_503(self, *_: object) -> None:
19+
with self.assertRaises(HTTPException) as ctx:
20+
get_inference_service_dep()
21+
22+
self.assertEqual(ctx.exception.status_code, 503)
23+
self.assertIn("Inference service unavailable", str(ctx.exception.detail))
24+
25+
26+
if __name__ == "__main__":
27+
unittest.main()
28+

0 commit comments

Comments
 (0)