Error Handling

Due to the multi-request nature of httpx, it chooses not to bubble up exceptions to user code. A call to HTTPX will therefore always return a response (if it doesn’t, it’s bug). However, the response might be encapsulating an error.

As of 0.0.3, you can choose to bubble up such exceptions by calling response.raise_for_status. This will raise an exception if the response wraps an exception or if the response contains an HTTP Error Status Code (4xx or 5xx).

response = HTTPX.get("")
response.raise_for_status #=> raises Errno::CONNREFUSED

response = HTTPX.get("") #=> 404
response.raise_for_status #=> raises HTTPX::HTTPError

response = HTTPX.get("") #=> 200
response.raise_for_status #=> nil

Next: DNS Resolvers