Problem Statement
Describe a clean error-handling strategy for a service layer. How do you map domain failures to API responses?
Explanation
Model failures explicitly as typed errors (e.g., ValidationError, NotFound, Conflict). Service methods return either a result or a failure. This keeps control flow readable and testable.
At the boundary, map types to HTTP codes and include stable error codes for clients. Log with correlation id, avoid leaking internals, and keep messages user-friendly for recoverable cases.
Code Solution
SolutionRead Only
Result<T, E extends DomainError> // map to 400/404/409/500 at controller
Practice Sets
This question appears in the following practice sets:
