Problem Statement
Describe an error-handling strategy across layers (API, service, data).
Explanation
Define custom exception types for domain errors and translate them at the API edge to proper HTTP codes. Log once with enough context but without secrets. Avoid logging the same error repeatedly in lower layers.
At the service layer, raise precise exceptions and keep stack traces. At the data layer, wrap low-level errors with cause to preserve context. This gives clean responses to clients and debuggable traces for engineers.
Code Solution
SolutionRead Only
class NotFoundError(Exception): ...
# API edge
try:
svc.get_user(id)
except NotFoundError:
return 404, {'error':'not found'}