Handle CLI request errors and backend whisper timeout
This commit is contained in:
@@ -64,6 +64,14 @@ def format_http_error(response: httpx.Response, endpoint: str) -> str:
|
||||
return f"HTTP {response.status_code} from {endpoint}: {body}"
|
||||
|
||||
|
||||
def format_request_error(exc: httpx.RequestError, endpoint: str) -> str:
|
||||
if isinstance(exc, httpx.TimeoutException):
|
||||
return f"Request to {endpoint} timed out."
|
||||
|
||||
reason = str(exc).strip() or exc.__class__.__name__
|
||||
return f"Request to {endpoint} failed: {reason}"
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = build_parser()
|
||||
args = parser.parse_args()
|
||||
@@ -75,16 +83,19 @@ def main() -> int:
|
||||
server = resolve_server(args)
|
||||
endpoint = f"{server}/transcriptions"
|
||||
|
||||
with input_file.open("rb") as handle, httpx.Client(timeout=300.0) as client:
|
||||
response = client.post(
|
||||
endpoint,
|
||||
data={
|
||||
"model": args.model,
|
||||
"language": args.language or "",
|
||||
"output_format": args.output_format,
|
||||
},
|
||||
files={"file": (input_file.name, handle, "application/octet-stream")},
|
||||
)
|
||||
try:
|
||||
with input_file.open("rb") as handle, httpx.Client(timeout=300.0) as client:
|
||||
response = client.post(
|
||||
endpoint,
|
||||
data={
|
||||
"model": args.model,
|
||||
"language": args.language or "",
|
||||
"output_format": args.output_format,
|
||||
},
|
||||
files={"file": (input_file.name, handle, "application/octet-stream")},
|
||||
)
|
||||
except httpx.RequestError as exc:
|
||||
parser.exit(1, f"{format_request_error(exc, endpoint)}\n")
|
||||
|
||||
try:
|
||||
response.raise_for_status()
|
||||
|
||||
@@ -44,3 +44,20 @@ def test_format_http_error_with_empty_body() -> None:
|
||||
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>"
|
||||
|
||||
|
||||
def test_format_request_error_timeout() -> None:
|
||||
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
|
||||
exc = httpx.ReadTimeout("read timed out", request=request)
|
||||
message = main.format_request_error(exc, "http://localhost:8000/transcriptions")
|
||||
assert message == "Request to http://localhost:8000/transcriptions timed out."
|
||||
|
||||
|
||||
def test_format_request_error_network_failure() -> None:
|
||||
request = httpx.Request("POST", "http://localhost:8000/transcriptions")
|
||||
exc = httpx.ConnectError("connection refused", request=request)
|
||||
message = main.format_request_error(exc, "http://localhost:8000/transcriptions")
|
||||
assert (
|
||||
message
|
||||
== "Request to http://localhost:8000/transcriptions failed: connection refused"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user