Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions sanctions/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,20 @@ async def screen(req: ScreenRequest):
"""Screen a name against OpenSanctions lists via yente."""
try:
results = await _query_yente(req.name, req.threshold)
except httpx.ConnectError:
except httpx.ConnectError as e:
raise HTTPException(
status_code=503,
detail=f"yente service not reachable at {YENTE_URL}.",
)
) from e
except httpx.TimeoutException as e:
raise HTTPException(
status_code=504,
detail=f"yente request timed out at {YENTE_URL}.",
) from e
except httpx.RequestError as e:
raise HTTPException(status_code=503, detail=f"yente request failed: {e}") from e
except httpx.HTTPStatusError as e:
raise HTTPException(status_code=502, detail=f"yente error: {e}")
raise HTTPException(status_code=502, detail=f"yente error: {e}") from e

matches = [
ScreenMatch(
Expand Down
18 changes: 18 additions & 0 deletions sanctions/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from unittest.mock import AsyncMock, patch

import httpx
import pytest
from fastapi.testclient import TestClient

Expand Down Expand Up @@ -77,6 +78,23 @@ async def test_screen_no_matches():
assert data["matches"] == []


def test_screen_timeout_returns_gateway_timeout():
"""Timeouts von yente werden als 504 statt als 500 zurueckgegeben."""
with patch(
"sanctions.src.main._query_yente",
new=AsyncMock(side_effect=httpx.ReadTimeout("timed out")),
):
response = client.post(
"/api/screen",
json={"name": "Max Mustermann"},
)

assert response.status_code == 504
assert response.json() == {
"detail": "yente request timed out at http://localhost:8100."
}


def test_screen_missing_body():
"""Fehlender Body → 422 Validation Error."""
response = client.post("/api/screen", json={})
Expand Down
Loading