Why this matters
External calls are unreliable; explicit handling avoids hanging and unclear states.
Wrap HTTP/SDK calls with timeouts and catch transport/errors; log context and map to domain failures.
External calls are unreliable; explicit handling avoids hanging and unclear states.
Side-by-side examples engineers can pattern-match during review.
r = requests.get(url)
return r.json()import requests
try:
r = requests.get(url, timeout=5)
r.raise_for_status()
return r.json()
except requests.Timeout:
logger.warning('timeout', extra={'url': url})
raise
except requests.RequestException as e:
logger.exception('http error', extra={'url': url})
raiserequests.post(url, data=payload)requests.post(url, json=payload, timeout=5)From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.