1.Exception boundaries
An exception boundary is the one deliberate place — an API handler, a job entry point — where every exception from the code it calls is caught and translated into one stable, intentional response, instead of leaking internal detail outward.
Key features
- A boundary sits at a system edge — an API handler, a queue consumer, a CLI entry point
- Code beneath the boundary raises specific, meaningful exceptions freely
- The boundary catches broadly and translates into one stable response shape
- Without a boundary, internal exception types and details leak to external callers
- Only one boundary is needed per entry point — do not scatter broad catches throughout
Command
def api_boundary(payload):
try:
return call_external_api(payload)
except ValueError as e:
raise ExternalAPIError(f"upstream call failed: {e}") from ePut one broad catch at the edge of a system — not scattered through internal code — and translate everything into a stable, intentional response there.