Merge pull request 'cli: show HTTP status/body on transcription backend failures' (#2) from fix/whisper-remote-http-error-message into main
Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
@@ -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 "<empty response body>"
|
||||
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:
|
||||
|
||||
@@ -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: <empty response body>"
|
||||
|
||||
Reference in New Issue
Block a user