Part 05 / Topic 4 of 7
Fail fast vs retry
The retry set is short: 429 and 5xx. Everything else fails fast, because the request itself is wrong and will be wrong again:
# examples/02_a_wrong_token_fails_fast_instead.py
import httpx
from fdelab import customer_api_transport
with httpx.Client(base_url="https://api.acme.example", transport=customer_api_transport(),
headers={"Authorization": "Bearer wrong"}) as client:
r = client.get("/v1/shipments")
print(r.status_code, r.json())
try:
r.raise_for_status()
except httpx.HTTPStatusError as e:
print("raised:", type(e).__name__, "- do not retry a 401; fix the credentials")
# 401 {'error': 'invalid token'}
# raised: HTTPStatusError - do not retry a 401; fix the credentials
Retrying a 401 is worse than useless: it hides a configuration problem behind minutes of backoff, and enough retries can lock the account. Fail fast, loudly, with the fix in the error message.
🧪 Quick quiz
- The API returns 401 for a wrong token. Should the client retry?
Show the answer
No — fail fast and fix the credentials. The example raises HTTPStatusError immediately instead of retrying. (verified against the example output above)
Done here? The practice set for Part 5 — 4 exercises with solutions — is on the part page.