From 2b1c26781e14a7f09161f057b6d8fde59697abe8 Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 25 May 2026 17:18:31 +0000 Subject: [PATCH] cli: surface HTTP status and body on backend errors --- cli/src/main.py | 7 ++++++- cli/tests/test_main.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/cli/src/main.py b/cli/src/main.py index 277c741..5ddbe54 100644 --- a/cli/src/main.py +++ b/cli/src/main.py @@ -59,6 +59,11 @@ def save_response(response: httpx.Response, destination: Path) -> None: destination.write_bytes(response.content) +def format_http_error(response: httpx.Response, endpoint: str) -> str: + body = response.text.strip() or "" + return f"HTTP {response.status_code} from {endpoint}: {body}" + + def main() -> int: parser = build_parser() args = parser.parse_args() @@ -84,7 +89,7 @@ def main() -> int: try: response.raise_for_status() except httpx.HTTPStatusError as exc: - message = exc.response.text.strip() or str(exc) + message = format_http_error(exc.response, endpoint) parser.exit(1, f"{message}\n") if args.to_file: diff --git a/cli/tests/test_main.py b/cli/tests/test_main.py index a77e9ab..b1c4c1e 100644 --- a/cli/tests/test_main.py +++ b/cli/tests/test_main.py @@ -8,6 +8,7 @@ import pytest sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) import main +import httpx def test_resolve_server_from_env(monkeypatch) -> None: @@ -29,3 +30,17 @@ def test_infer_output_path_for_directory(tmp_path: Path) -> None: def test_infer_output_path_for_explicit_file(tmp_path: Path) -> None: destination = main.infer_output_path(tmp_path / "custom-name.txt", Path("clip.wav"), "txt") assert destination == tmp_path / "custom-name.txt" + + +def test_format_http_error_with_body() -> None: + request = httpx.Request("POST", "http://localhost:8000/transcriptions") + response = httpx.Response(500, text="Internal Server Error", request=request) + message = main.format_http_error(response, "http://localhost:8000/transcriptions") + assert message == "HTTP 500 from http://localhost:8000/transcriptions: Internal Server Error" + + +def test_format_http_error_with_empty_body() -> None: + request = httpx.Request("POST", "http://localhost:8000/transcriptions") + response = httpx.Response(500, text="", request=request) + message = main.format_http_error(response, "http://localhost:8000/transcriptions") + assert message == "HTTP 500 from http://localhost:8000/transcriptions: "