feat: keep the gateway's words on the OCR branches too

Issue #10 Task 4: the OCR side said only 'HTTP 404'. The issue reported
the chat path, but the batch that lost its 400 was reading tables - the
same blind spot, one transport over. Reuses the shared summarizer.
This commit is contained in:
2026-08-16 06:14:07 -04:00
parent 0edb9d397a
commit a3f4cc323f
2 changed files with 52 additions and 2 deletions
+39 -1
View File
@@ -19,7 +19,11 @@ from polygateway.errors import (
SourceDeadError,
TransientError,
)
from polygateway.transports.monkey_ocr import MonkeyOcrTransport, _parse_middle_json
from polygateway.transports.monkey_ocr import (
MonkeyOcrTransport,
_classify_status,
_parse_middle_json,
)
from polygateway.types import SourceConfig
@@ -306,6 +310,40 @@ class TestErrorTranslation:
await t.recognize_text(image=b"jpg", source=_source(), call_id="c1")
assert ei.value.status_code == status
@pytest.mark.parametrize(
("status", "exc_type"),
[(502, TransientError), (401, SourceDeadError), (404, RequestRejectedError)],
)
async def test_body_survives_every_branch(self, status, exc_type):
"""issue #10: OCR 侧 message 原本只有 HTTP 状态码,拒绝理由同样丢失。"""
body = '{"detail":"unsupported image mode CMYK"}'
t = _transport_for(_routes(text_resp=httpx.Response(status, content=body.encode())))
with pytest.raises(exc_type) as ei:
await t.recognize_text(image=b"jpg", source=_source(), call_id="c1")
assert ei.value.body_text == body
assert str(ei.value).endswith(f" | {body}")
def test_unread_body_degrades_without_changing_class(self):
"""取不到 body 时降级空串: 绝不能让 ResponseNotRead 逃出错误四分类。
直接测纯函数而非走 MockTransport——真实客户端对非 stream 请求总会读完
响应,未读态只可能在将来给 OCR 加 stream 时出现,而那正是要防的场景。
"""
class _Unread(httpx.SyncByteStream):
def __iter__(self):
yield b"body"
exc = httpx.HTTPStatusError(
"404",
request=httpx.Request("POST", "http://ocr.example/ocr/text"),
response=httpx.Response(404, stream=_Unread()),
)
err = _classify_status(exc, "monkey_1", "text")
assert isinstance(err, RequestRejectedError)
assert err.body_text == ""
assert str(err) == "monkey_1 OCR text HTTP 404"
async def test_connect_error_transient(self):
def handler(request):
raise httpx.ConnectError("refused", request=request)