ADR-006: Granular Exception HierarchyΒΆ
Status: β Accepted Date: 2026-01-29 Deciders: Rodrigo RoldΓ‘n
ContextΒΆ
Error handling can be:
Generic: A single
HttpErrortypeGranular: Specific exceptions for each error type
Very granular: Sub-exceptions for each case
Benefits of granularity:
Allows selective catching
Better recovery strategies
Easier debugging
Clearer documentation
DecisionΒΆ
Implement granular exception hierarchy with 3 levels:
ReqivoError (base)
βββ RequestError
β βββ NetworkError
β β βββ TlsError
β βββ TimeoutError
β β βββ ConnectTimeout
β β βββ ReadTimeout
β βββ ProtocolError
β β βββ InvalidResponseError
β βββ RedirectLoopError
Usage:
# Specific catch
try:
response = session.get(url)
except ConnectTimeout:
# Retry with higher timeout
response = session.get(url, timeout=30)
except TlsError:
# Log security issue
logger.error("TLS handshake failed")
raise
except NetworkError:
# Fallback to another URL
response = session.get(fallback_url)
# Generic catch
except ReqivoError:
# Handle any Reqivo error
pass
Principles:
Each exception represents a distinct failure mode
Inheritance allows catching at different levels
Descriptive names (
ConnectTimeoutvsTimeout)Optional context info without sensitive data
ConsequencesΒΆ
Positive β ΒΆ
Recovery strategies: Code can react specifically
Debugging: Clearer stack traces
Documentation: API docs show what can fail
Type safety: mypy can verify exception handling
Testable: Easy to test each error path
Negative βΒΆ
More code: More classes to maintain
Documentation: Each exception must be documented
Breaking changes: Adding exceptions can break existing code
MitigationsΒΆ
Stable hierarchy: Donβt change inheritance after v1.0
Complete documentation: Each exception with docstring
Semantic versioning: Breaking changes only in major versions
Alternatives ConsideredΒΆ
Only ReqivoError: Rejected. Makes recovery difficult.
String error codes: Rejected. Not type-safe.
Stdlib exceptions: Rejected. Not specific to HTTP.
ReferencesΒΆ
PEP 3151: Reworking the OS and IO exception hierarchy
requests.exceptions