Error Architecture
Domain Errors
Domain errors represent business logic violations and validation failures:src/errors/mod.rs
- Invalid email format
- Username too short
- Business rule violations
- Value object validation failures
API Errors
API errors map to HTTP status codes and provide structured responses:src/errors/mod.rs
Error Conversion
Automatic Domain to API Error Conversion
Domain errors automatically convert to API validation errors:src/errors/mod.rs
? operator throughout your application:
Error Responses
Response Structure
All errors return a consistent JSON structure:src/errors/mod.rs
HTTP Status Code Mapping
src/errors/mod.rs
Example Responses
404 Not FoundError Handling Patterns
Type Alias for Results
src/errors/mod.rs
Controller Error Handling
Controllers can returnResult<HttpResponse, ApiError> directly:
Database Error Mapping
JWT Error Handling
Error Propagation
Layer-by-Layer Propagation
Repository LayerCustom Error Creation
Creating Domain Errors
Creating API Errors
Best Practices
Logging Errors
Error Recovery
Testing Error Handling
Next Steps
Middleware
Learn about request/response interception
Logging
Configure structured logging