Error Forge is a comprehensive error management framework for Rust applications that combines robust error handling with resilience patterns. It provides both synchronous and asynchronous error management capabilities, recovery patterns like circuit breakers and retry policies with backoff strategies, and error collection mechanisms. Error Forge simplifies error handling through expressive macros, automatic trait implementations, extensible error hooks, and resilience patterns designed for enterprise-grade applications.
Features
- Rich Error Types: Define expressive error types with minimal boilerplate
- ForgeError Trait: Unified interface for all error types with contextual metadata
- Async Error Support: First-class support for async applications with
AsyncForgeError
trait
- Error Recovery Patterns:
- Circuit Breaker: Prevent cascading failures with intelligent state management
- Retry Policies: Configurable retry mechanisms with predicate support
- Backoff Strategies: Exponential, linear, and fixed backoff with optional jitter
- Declarative Macros: Generate complete error enums with the
define_errors!
macro
- Error Composition: Combine errors from multiple modules with the
group!
macro
- Derive Macros: Quickly implement errors with
#[derive(ModError)]
- Console Formatting: ANSI color formatting for terminal output with
ConsoleTheme
- Error Hooks: Thread-safe error hook system using
OnceLock
- Structured Context: Error wrapping with context via
context()
and with_context()
methods
- Error Registry: Support for error codes and documentation URLs
- Non-fatal Error Collection: Collect and process multiple errors with
ErrorCollector
- Logging Integration: Optional integration with
log
and tracing
crates
- Cross-Platform: Full support for Linux, macOS, and Windows
- Zero Core Dependencies: Core functionality has no third-party dependencies
Installation
Add the following to your Cargo.toml
file:
[dependencies]
error-forge = "0.9.6"
Usage
Basic Error Definition
use error_forge::define_errors;
define_errors! {
pub enum DatabaseError {
#[error(display = "Database connection failed: {}", message)]
ConnectionFailed { message: String },
#[error(display = "Query execution failed: {}", message)]
QueryFailed { message: String, query: String },
#[error(display = "Record not found with ID: {}", id)]
RecordNotFound { id: String },
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let error = DatabaseError::connection_failed("Timeout after 30 seconds");
println!("Error kind: {}", error.kind());
println!("Caption: {}", error.caption());
println!("Status code: {}", error.status_code());
Err(Box::new(error))
}
Error Composition with the group!
Macro
use error_forge::{define_errors, group};
define_errors! {
pub enum ApiError {
#[error(display = "Invalid API key")]
InvalidApiKey,
#[error(display = "Rate limit exceeded")]
RateLimitExceeded,
}
}
define_errors! {
pub enum ValidationError {
#[error(display = "Required field {} is missing", field)]
MissingField { field: String },
#[error(display = "Field {} has invalid format", field)]
InvalidFormat { field: String },
}
}
group! {
pub enum AppError {
Api(ApiError),
Validation(ValidationError),
}
}
fn validate_request() -> Result<(), AppError> {
let error = ValidationError::missing_field("username");
Err(error.into())
}
Using Derive Macro
use error_forge::ModError;
#[derive(Debug, ModError)]
#[module_error(kind = "AuthError")]
pub enum AuthError {
#[error(display = "Invalid credentials")]
InvalidCredentials,
#[error(display = "Account locked: {}", reason)]
AccountLocked { reason: String },
#[error(display = "Session expired")]
#[http_status(401)]
SessionExpired,
}
fn login() -> Result<(), AuthError> {
Err(AuthError::InvalidCredentials)
}
Error Hooks for Logging Integration
use error_forge::{AppError, macros::{register_error_hook, ErrorLevel, ErrorContext}};
fn main() {
register_error_hook(|ctx| {
match ctx.level {
ErrorLevel::Info => println!("INFO: {} [{}]", ctx.caption, ctx.kind),
ErrorLevel::Warning => println!("WARN: {} [{}]", ctx.caption, ctx.kind),
ErrorLevel::Error => println!("ERROR: {} [{}]", ctx.caption, ctx.kind),
ErrorLevel::Critical => {
println!("CRITICAL: {} [{}]", ctx.caption, ctx.kind);
if ctx.is_fatal {
send_notification("Critical error occurred", ctx.caption);
}
}
}
});
let _error = AppError::config("Configuration file not found");
}
fn send_notification(level: &str, message: &str) {
println!("Notification sent: {} - {}", level, message);
}
Console Formatting
use error_forge::{AppError, ConsoleTheme};
fn main() {
let error = AppError::config("Database configuration missing");
let theme = ConsoleTheme::new();
println!("{}", theme.format_error(&error));
theme.install_panic_hook();
panic!("Something went wrong!");
}
Structured Context Support
use error_forge::{define_errors, context::ContextError};
use std::fs::File;
define_errors! {
pub enum FileError {
#[error(display = "Failed to open file")]
OpenFailed,
#[error(display = "Failed to read file")]
ReadFailed,
}
}
fn read_config_file(path: &str) -> Result<String, ContextError<FileError>> {
let mut file = File::open(path)
.map_err(|_| FileError::OpenFailed)
.with_context(format!("Opening config file: {}", path))?;
let mut contents = String::new();
file.read_to_string(&mut contents)
.map_err(|_| FileError::ReadFailed)
.context("Reading configuration data")?;
Ok(contents)
}
fn main() {
match read_config_file("/etc/app/config.json") {
Ok(config) => println!("Config loaded: {} bytes", config.len()),
Err(e) => {
println!("Error: {}", e);
println!("Original error: {}", e.source());
}
}
}
Error Collection
use error_forge::{define_errors, collector::ErrorCollector};
define_errors! {
pub enum ValidationError {
#[error(display = "Field '{}' is required", field)]
Required { field: String },
#[error(display = "Field '{}' must be a valid email", field)]
InvalidEmail { field: String },
}
}
fn validate_form(data: &FormData) -> Result<(), ValidationError> {
let mut collector = ErrorCollector::new();
if data.name.is_empty() {
collector.push(ValidationError::required("name"));
}
if data.email.is_empty() {
collector.push(ValidationError::required("email"));
} else if !is_valid_email(&data.email) {
collector.push(ValidationError::invalid_email("email"));
}
collector.into_result()
}
Advanced Usage
Microservice Error Management with Resilience
This example demonstrates a complete error management system for a microservice architecture with resilience patterns.
use error_forge::{define_errors, group, recovery::{CircuitBreaker, CircuitBreakerConfig, RetryPolicy, ForgeErrorRecovery}};
use std::time::Duration;
define_errors! {
pub enum DatabaseError {
#[error(display = "Failed to connect to database: {}", message)]
#[kind(Connection, retryable = true, status = 503)]
ConnectionFailed { message: String },
#[error(display = "Query execution failed: {}", message)]
#[kind(Query, retryable = true, status = 500)]
QueryFailed { message: String },
#[error(display = "Transaction failed: {}", message)]
#[kind(Transaction, retryable = true, status = 500)]
TransactionFailed { message: String },
}
}
define_errors! {
pub enum ApiError {
#[error(display = "External API request to {} failed: {}", endpoint, message)]
#[kind(Request, retryable = true, status = 502)]
RequestFailed { endpoint: String, message: String },
#[error(display = "Rate limit exceeded for endpoint: {}", endpoint)]
#[kind(RateLimit, retryable = true, status = 429)]
RateLimited { endpoint: String },
#[error(display = "Authentication failed: {}", message)]
#[kind(Auth, retryable = false, status = 401)]
AuthFailed { message: String },
}
}
define_errors! {
pub enum CacheError {
#[error(display = "Failed to connect to cache server: {}", message)]
#[kind(Connection, retryable = true, status = 503)]
ConnectionFailed { message: String },
#[error(display = "Cache operation failed: {}", message)]
#[kind(Operation, retryable = true, status = 500)]
OperationFailed { message: String },
}
}
group! {
pub enum ServiceError {
Database(DatabaseError),
Api(ApiError),
Cache(CacheError),
}
}
impl ForgeErrorRecovery for ServiceError {}
async fn get_user_data(user_id: &str) -> Result<UserData, ServiceError> {
let db_circuit_breaker = CircuitBreaker::new(CircuitBreakerConfig::default()
.with_failure_threshold(3)
.with_reset_timeout(Duration::from_secs(30)));
let db_retry_policy = RetryPolicy::new_exponential()
.with_max_retries(3)
.with_initial_delay(100)
.with_max_delay(2000)
.with_jitter(0.1);
let user = db_circuit_breaker.execute(|| async {
db_retry_policy.forge_executor().retry(|| async {
match database::query_user(user_id).await {
Ok(user) => Ok(user),
Err(e) => Err(DatabaseError::query_failed(e.to_string()).into())
}
}).await
}).await?;
let api_circuit_breaker = CircuitBreaker::new(CircuitBreakerConfig::default()
.with_failure_threshold(5)
.with_reset_timeout(Duration::from_secs(60)));
let user_preferences = api_circuit_breaker.execute(|| async {
let api_retry = RetryPolicy::new_exponential()
.with_max_retries(3)
.with_initial_delay(200)
.with_max_delay(5000);
api_retry.forge_executor().retry(|| async {
match external_api::fetch_user_preferences(user_id).await {
Ok(prefs) => Ok(prefs),
Err(e) => {
if e.contains("rate limit") {
Err(ApiError::rate_limited("preferences-api.example.com").into())
} else {
Err(ApiError::request_failed(
"preferences-api.example.com",
e.to_string()
).into())
}
}
}
}).await
}).await?;
Ok(UserData {
profile: user,
preferences: user_preferences,
})
}
struct UserData {
profile: User,
preferences: UserPreferences,
}
struct User { }
struct UserPreferences { }
mod database {
use super::*;
pub async fn query_user(_id: &str) -> Result<User, String> {
Ok(User {})
}
}
mod external_api {
use super::*;
pub async fn fetch_user_preferences(_id: &str) -> Result<UserPreferences, String> {
Ok(UserPreferences {})
}
}
Comprehensive Error Collection for Validation
This example shows how to use the error collector for complex data validation scenarios:
use error_forge::{define_errors, collector::ErrorCollector};
use std::collections::HashMap;
define_errors! {
pub enum ValidationError {
#[error(display = "Field '{}' is required", field)]
#[kind(Required, status = 400)]
Required { field: String },
#[error(display = "Field '{}' has invalid format: {}", field, message)]
#[kind(Format, status = 400)]
InvalidFormat { field: String, message: String },
#[error(display = "Field '{}' has invalid value: {}", field, message)]
#[kind(Value, status = 400)]
InvalidValue { field: String, message: String },
#[error(display = "Reference to '{}' not found", reference)]
#[kind(Reference, status = 400)]
InvalidReference { reference: String },
}
}
struct UserProfile {
username: String,
email: Option<String>,
age: Option<u8>,
country: Option<String>,
preferences: HashMap<String, String>,
}
fn validate_user_profile(profile: &UserProfile) -> Result<(), ValidationError> {
let mut collector = ErrorCollector::new();
if profile.username.is_empty() {
collector.push(ValidationError::required("username"));
} else if profile.username.len() < 3 || profile.username.len() > 30 {
collector.push(ValidationError::invalid_format(
"username",
"Username must be between 3 and 30 characters long"
));
}
if let Some(email) = &profile.email {
if !email.contains('@') || !email.contains('.') {
collector.push(ValidationError::invalid_format(
"email",
"Email must contain '@' and domain"
));
}
}
if let Some(age) = profile.age {
if age < 13 {
collector.push(ValidationError::invalid_value(
"age",
"User must be at least 13 years old"
));
}
}
if let Some(country) = &profile.country {
let valid_countries = vec!["US", "CA", "UK", "AU", "DE", "FR"];
if !valid_countries.contains(&country.as_str()) {
collector.push(ValidationError::invalid_reference(
format!("country '{}'", country)
));
}
}
for (key, value) in &profile.preferences {
match key.as_str() {
"theme" => {
let valid_themes = vec!["light", "dark", "system"];
if !valid_themes.contains(&value.as_str()) {
collector.push(ValidationError::invalid_value(
"preferences.theme",
format!("'{}' is not a valid theme", value)
));
}
},
"notifications" => {
let valid_values = vec!["all", "important", "none"];
if !valid_values.contains(&value.as_str()) {
collector.push(ValidationError::invalid_value(
"preferences.notifications",
format!("'{}' is not a valid notification setting", value)
));
}
},
_ => {
collector.push(ValidationError::invalid_reference(
format!("preference '{}'", key)
));
}
}
}
collector.into_result()
}
For more detailed documentation and additional advanced usage examples, refer to the API Documentation.