Separate HTTP status from transport failure
An HTTP error response and a failed connection are different events. A service can return a valid HTTP 503 response, while a timeout may produce no response at all. HTTPX exposes separate exception families for these cases.
Call raise_for_status when unsuccessful status codes should become exceptions. Catch the specific family whose contract you want to convert, and leave unexpected programming errors visible. An injected MockTransport can generate both outcomes without internet access or waiting for a real timeout. The resulting function can return a small explicit status for its caller.
import httpx
def handler(request):
return httpx.Response(503)
with httpx.Client(transport=
httpx.MockTransport(handler)) as client:
response = client.get("https://demo.test")
try:
response.raise_for_status()
except httpx.HTTPStatusError as error:
print(error.response.status_code)Your task
Implement probe(transport): GET https://demo.test/health. Return "ok" for success, "http:<status>" after HTTPStatusError, or "network" after RequestError. Do not convert unrelated exceptions.
The explanation opens here once every check passes.