RE

Result Types

Functional error handling using Result/Either/Optional types

Details

Language / Topic
_UUniversal
Category
Error Handling

Rules

balanced
- Use Result/Either types for operations that can fail expectedly. Reserve exceptions for truly unexpected failures.
- Return typed errors: Result<User, ValidationError | NotFoundError>, not Result<User, Error>.
- Transform errors at layer boundaries — low-level IO errors become domain-specific errors (e.g., `UserNotFound`) as they propagate up.
- Chain Result transformations with .map()/.flatMap() instead of nested if/else or try/catch.
- Define specific error types for different failure modes — callers need to handle each case.
- Use discriminated unions or tagged types for error variants that need different handling.
- Log errors at the boundary where Results are unwrapped, not at every intermediate step.