In this lesson, you will learn how to build resilient API clients by proactively managing HTTP status codes and network-level exceptions to prevent your application from crashing during external service outages.
Beyond Simple Requests
When consuming RESTful APIs using the requests library, a successful call isn't guaranteed. Even if your network is stable, the remote server might return a 4xx (client error) or 5xx (server error). Relying on default behavior often means these errors pass silently, leading to downstream data corruption when you try to parse a non-existent JSON payload.
To handle this effectively, you must combine explicit status code validation with exception handling for network interruptions.
The Robust Pattern
The requests library provides a helper method called raise_for_status(), which automatically raises an HTTPError if the response contains a 4xx or 5xx code. By wrapping your logic in a try...except block, you can distinguish between "API logic errors" and "infrastructure failures" (like Timeout or ConnectionError).
import requests
from requests.exceptions import HTTPError, Timeout, ConnectionError
def fetch_data(url):
try:
response = requests.get(url, timeout=5)
# Raises an HTTPError for bad responses
response.raise_for_status()
return response.json()
except HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except Timeout:
print("The request timed out. Retrying might be appropriate.")
except ConnectionError:
print("Failed to connect to the server.")
except Exception as err:
print(f"An unexpected error occurred: {err}")
return None
Why this Matters
In a distributed system, relying on the assumption that a request will succeed is a recipe for technical debt. By implementing this pattern, you ensure that your integration layer fails predictably. This allows you to log specific failure modes—such as distinguishing between a server that is down (ConnectionError) versus a server that is simply rejecting your credentials (HTTPError)—which is critical for maintaining healthy services and performing effective remote debugging.
Key Takeaway: Always pair raise_for_status() with specific exception handling to differentiate between recoverable network issues and definitive API errors.