fix(vision): worker down o in timeout non esce più come 500 nudo (#14)

_call_worker distingue httpx.TimeoutException (504) dal resto dei
TransportError/HTTPError (502, RemoteProtocolError incluso). Un 422 del
worker si propaga come 422 con il motivo del worker, non più 502 generico.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-21 22:34:06 +02:00
parent a330f2b340
commit 67392a946c
2 changed files with 98 additions and 12 deletions
@@ -115,3 +115,75 @@ async def test_a_well_formed_response_still_passes_through(monkeypatch):
report = await _call_worker(b"img", {"schema_version": 1, "tools": []})
assert report == body
# ---------------------------------------------------------------------------
# #14: the worker dying mid-response (RemoteProtocolError) or a connect
# timeout (ConnectTimeout, an httpx.TimeoutException) used to fall through
# every except clause here and surface as a naked 500.
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_the_worker_dying_mid_response_answers_502_not_a_naked_500(monkeypatch):
_install(
monkeypatch,
raises=httpx.RemoteProtocolError(
"Server disconnected without sending a response."
),
)
with pytest.raises(HTTPException) as excinfo:
await _call_worker(b"img", {"schema_version": 1, "tools": []})
assert excinfo.value.status_code == 502
assert "unreachable" in excinfo.value.detail.lower()
@pytest.mark.asyncio
async def test_a_connect_timeout_answers_504(monkeypatch):
"""ConnectTimeout is an httpx.TimeoutException, not caught by name before -
only httpx.ReadTimeout was."""
_install(monkeypatch, raises=httpx.ConnectTimeout("connect timed out"))
with pytest.raises(HTTPException) as excinfo:
await _call_worker(b"img", {"schema_version": 1, "tools": []})
assert excinfo.value.status_code == 504
assert "time" in excinfo.value.detail.lower()
# ---------------------------------------------------------------------------
# #15: a 422 from the worker (bad image, malformed graph) must reach the
# Maker as a 422 with the worker's own reason, not a flattened 502.
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_a_422_from_the_worker_propagates_as_422_with_its_detail(monkeypatch):
_install(
monkeypatch,
response=_FakeResponse(
422, body={"detail": "image is not decodable: cannot identify image file"},
),
)
with pytest.raises(HTTPException) as excinfo:
await _call_worker(b"img", {"schema_version": 1, "tools": []})
assert excinfo.value.status_code == 422
assert "not decodable" in excinfo.value.detail
@pytest.mark.asyncio
async def test_a_422_from_the_worker_without_json_body_still_propagates(monkeypatch):
_install(
monkeypatch,
response=_FakeResponse(422, text="plain text reason", raise_on_json=True),
)
with pytest.raises(HTTPException) as excinfo:
await _call_worker(b"img", {"schema_version": 1, "tools": []})
assert excinfo.value.status_code == 422
assert excinfo.value.detail == "plain text reason"