# Error Handling in DDD This document defines error handling principles that apply across all layers and languages. ## Core Principles 1. **No Silent Failures** - All errors must be explicitly handled or propagated 2. **Layer-Specific Errors** - Each layer defines its own error types 3. **Error Transformation** - Errors are transformed at layer boundaries 4. **Proper Logging** - Original errors logged before transformation 5. **Result Types Over Exceptions** - Prefer Result types (where supported) over exceptions ## Language-Specific Implementation - **Java**: See [languages/java/error-handling.md](../languages/java/error-handling.md) - **Go**: Use error returns and custom error types ## Error Hierarchy ``` Domain Errors (business rule violations) ↓ transformed at boundary Application Errors (use case failures) ↓ transformed at boundary Infrastructure Errors (technical failures) ``` ## Logging Strategy - **ERROR**: Infrastructure failures (database down, network error) - **WARN**: Business rule violations (insufficient funds, invalid state) - **INFO**: Normal operations (order placed, account created) - **DEBUG**: Detailed flow information - **TRACE**: Original errors when transforming between layers ## Exception Boundary **Infrastructure Layer** is the exception boundary: - Infrastructure catches external exceptions (SQL, HTTP, etc.) - Transforms to domain error types - Logs original exception at ERROR level - Returns domain error type **Domain and Application** layers: - Never throw exceptions (use Result types or errors) - Only work with domain/application error types - No try/catch blocks needed