Appearance
⚠️ Error & Exception Handling Standards for Flutter
Table of Contents
- Core Standards
- Failure Types
- Error Handling Patterns
- Network Error Handling
- UI Error Handling
- Logging and Monitoring
- Common Violations
Core Standards
Error Handling Requirements
- Always use structured error handling with Failure classes
- Implement proper error mapping for different error types
- Use safeCall wrapper for network operations
- Handle errors gracefully in UI components
- Log errors appropriately with context
- Provide meaningful error messages to users
- Implement proper error recovery mechanisms
- Use Result pattern for error handling
Error Handling Patterns
- Use Result pattern for operations that can fail
- Implement proper error mapping from exceptions to failures
- Use safeCall wrapper for network operations
- Handle errors at appropriate levels (UI, Service, Repository)
- Provide fallback mechanisms for critical operations
- Use proper error propagation through the call stack
Failure Types
Failure Classification Rules
- ServerFailure - Server-side errors (5xx, 4xx responses)
- ConnectionFailure - Network connectivity issues
- DevelopmentFailure - Development-time errors and bugs
- ClientFailure - Client-side validation and logic errors
- Use specific failure types for different error scenarios
- Implement proper error mapping from exceptions to failures
Custom Failure Types
- Create specific failure types for different error scenarios
- Implement proper error context and additional data
- Use failure extensions for common operations
- Implement proper error serialization for logging
Error Handling Patterns
Repository Pattern Error Handling
- Use safeCall wrapper for all network operations
- Implement proper error mapping from exceptions to failures
- Handle different error types appropriately
- Provide fallback mechanisms for critical operations
- Use proper error propagation through the call stack
Service Layer Error Handling
- Implement proper error handling in service methods
- Use repository error handling patterns
- Provide meaningful error messages for business logic
- Handle validation errors appropriately
- Implement proper error recovery mechanisms
Network Error Handling
Dio Error Handling Rules
- Use safeCall wrapper for all network operations
- Implement proper error mapping from DioException to Failure
- Handle different DioException types appropriately
- Provide meaningful error messages for users
- Implement proper error recovery mechanisms
Network Error Recovery Rules
- Implement retry mechanisms for transient errors
- Use exponential backoff for retry attempts
- Handle connection failures gracefully
- Provide offline fallbacks when possible
- Implement proper error recovery strategies
UI Error Handling
Cubit Error Handling Rules
- Handle errors in Cubit methods appropriately
- Emit error states with proper error information
- Provide meaningful error messages for users
- Implement proper error recovery mechanisms
- Use proper error logging with context
Widget Error Handling Rules
- Handle error states in UI components
- Provide user-friendly error messages and recovery options
- Implement proper error boundaries for critical components
- Use error widgets for consistent error display
- Implement proper error recovery mechanisms
Form Error Handling Rules
- Handle validation errors in form fields
- Display field-specific error messages appropriately
- Implement proper error recovery mechanisms
- Use consistent error styling across forms
- Provide clear error feedback to users
Logging and Monitoring
Error Logging Rules
- Log errors with proper context and stack traces
- Use appropriate log levels for different error types
- Include relevant metadata in error logs
- Implement proper error aggregation for monitoring
- Use structured logging for better analysis
Error Monitoring Rules
- Implement proper error tracking for production
- Use crash reporting services for error monitoring
- Track error rates and patterns
- Implement proper error alerting for critical errors
- Use error analytics for improvement
Common Violations
❌ DO NOT Violate These Rules
- Don't ignore errors - Always handle errors appropriately
- Don't use generic error messages - Provide specific, helpful messages
- Don't expose technical details - Use user-friendly error messages
- Don't forget error logging - Always log errors with context
- Don't use try-catch without proper handling - Implement proper error handling
- Don't ignore network errors - Handle network failures gracefully
- Don't use empty catch blocks - Always handle caught exceptions
- Don't forget error recovery - Implement proper error recovery mechanisms
- Don't ignore validation errors - Handle validation failures appropriately
- Don't use hardcoded error messages - Use localized error messages
- Don't swallow exceptions - Always handle or rethrow exceptions
- Don't ignore error states - Always handle error states in UI
- Don't use print for errors - Use proper logging mechanisms
- Don't forget error boundaries - Implement error boundaries for critical components
- Don't ignore retry mechanisms - Implement retry for transient errors
✅ ALWAYS Follow These Rules
- Use structured error handling with Failure classes
- Implement proper error mapping for different error types
- Use safeCall wrapper for network operations
- Handle errors gracefully in UI components
- Log errors appropriately with context
- Provide meaningful error messages to users
- Implement proper error recovery mechanisms
- Use Result pattern for error handling
- Handle different failure types appropriately
- Implement proper error propagation through the call stack
- Use proper error logging with stack traces
- Implement error monitoring for production
- Handle validation errors appropriately
- Use proper error boundaries for critical components
- Implement retry mechanisms for transient errors
- Use exponential backoff for retry attempts
- Handle connection failures gracefully
- Provide offline fallbacks when possible
- Use proper error recovery strategies
- Implement proper error alerting for critical errors
- Use user-friendly error messages in UI
- Implement proper error state management in Cubit/Bloc
- Handle errors at appropriate levels (UI, Service, Repository)
- Use consistent error handling patterns across the app
- Implement proper error serialization for logging
- Use proper error context in error messages
- Implement proper error aggregation for monitoring
- Use structured logging for better analysis
- Implement proper error alerting for critical errors
- Use error analytics for improvement