Timeouts and network errors
Sometimes no response comes at all: the server is down, or it never finishes answering. Those are exceptions, not status codes, and a hang needs a timeout.
import httpx2
def server_is_down(request):
raise httpx2.ConnectError("connection refused", request=request)
client = httpx2.Client(transport=httpx2.MockTransport(server_is_down), base_url="https://api.example.com")
client.get("/health")The fake server from lesson 3 raises the exception httpx2 raises when nothing is listening. There is no response, so there is no status code to check: the call itself fails.
Answers that never come
A server that accepts the connection and then hangs is worse, because nothing fails. timeout= sets how many seconds the client waits before giving up:
def server_hangs(request):
raise httpx2.ReadTimeout("no answer within 10 seconds", request=request)
client = httpx2.Client(transport=httpx2.MockTransport(server_hangs), base_url="https://api.example.com", timeout=10.0)
try:
client.get("/v1/chat/completions")
except httpx2.TimeoutException as error:
print(type(error).__name__, "-", error)This handler raises the exception a real timeout produces, since a function cannot be slow on purpose without making you wait. httpx2 waits five seconds by default. Model calls that write long answers can take longer, so set the timeout to fit the call, not the default.
Catching both
def safe_get(client, path):
try:
response = client.get(path)
except httpx2.TransportError as error:
return f"no response: {type(error).__name__}"
return f"status {response.status_code}"
for handler in (server_is_down, server_hangs):
client = httpx2.Client(transport=httpx2.MockTransport(handler), base_url="https://api.example.com")
print(safe_get(client, "/health"))TransportError covers every way a request can fail to get a response, timeouts and connection errors included. Two kinds of failure, two kinds of handling: an exception means nothing came back, a status code means something did.
- Make a handler that returns a 503 and pass it to
safe_get. Which branch reports it? - Catch only
httpx2.ConnectErrorand run the loop. - Pass
timeout=httpx2.Timeout(10.0, connect=2.0): a short wait to connect, a longer one for the answer.
You understood something today that you didn't yesterday.