Crates.io | decrust-core |
lib.rs | decrust-core |
version | 1.2.6 |
created_at | 2025-05-25 19:14:57.812296+00 |
updated_at | 2025-05-25 19:30:07.090118+00 |
description | Core error handling framework for Decrust |
homepage | |
repository | https://github.com/arcmoonstudios/decrust |
max_upload_size | |
id | 1688614 |
size | 842,059 |
Advanced Error Handling Framework for Rust - Core Components
Decrust-Core is the foundation of the Decrust error handling framework, providing comprehensive, production-ready error handling with rich error context, automatic error recovery, circuit breaker patterns, and powerful debugging capabilities.
use decrust_core::{DecrustError, DecrustResultExt, DecrustOptionExt, oops, validation_error};
// Basic error creation with rich context
fn process_user_data(data: Option<&str>) -> Result<String, DecrustError> {
let user_data = data.decrust_ok_or_missing_value("user data")?;
if user_data.is_empty() {
return Err(validation_error!("user_data", "Data cannot be empty"));
}
// Simulate an IO operation that might fail
std::fs::read_to_string("config.json")
.map_err(|e| oops!("Failed to read configuration", e))
.and_then(|_| Ok(format!("Processed: {}", user_data)))
}
Every error includes comprehensive context with location tracking, severity levels, and metadata for better debugging and monitoring.
use decrust_core::{error_context, types::ErrorSeverity, oops};
// Create rich error context with metadata
let context = error_context!(
"Database connection failed",
severity: ErrorSeverity::Critical
).with_component("database")
.with_correlation_id("req-123")
.with_recovery_suggestion("Check database connectivity");
Built-in circuit breaker for handling external service failures gracefully.
use decrust_core::{circuit_breaker::{CircuitBreaker, CircuitBreakerConfig}, DecrustError};
use std::time::Duration;
// Configure circuit breaker
let config = CircuitBreakerConfig {
failure_threshold: 5,
reset_timeout: Duration::from_secs(30),
operation_timeout: Some(Duration::from_secs(5)),
..Default::default()
};
let circuit_breaker = CircuitBreaker::new("external-api", config);
// Execute operations through circuit breaker
let result = circuit_breaker.execute(|| {
external_api_call()
});
Smart error recovery with configurable retry strategies and fix suggestions.
use decrust_core::{DecrustError, decrust::{Decrust, AutocorrectableError}};
let mut decrust = Decrust::new();
// Apply fixes automatically
if let Some(fix) = decrust.suggest_autocorrection(&error, None) {
println!("Suggested fix: {}", fix.description);
}
Ergonomic macros for common error handling patterns.
use decrust_core::{oops, validation_error, error_context, location, types::ErrorSeverity};
// Quick error creation
let error = oops!("Something went wrong", source_error);
// Validation errors with suggestions
let validation_err = validation_error!(
"email",
"Invalid email format",
suggestion: "Use format: user@domain.com"
);
// Rich context with location tracking
let context = error_context!("Operation failed", severity: ErrorSeverity::Error);
let loc = location!(context: "user authentication", function: "login");
Pre-built error variants for common scenarios with rich metadata.
use decrust_core::{DecrustError, Backtrace, OptionalError};
// Network errors with retry information
let network_error = DecrustError::Network {
source: Box::new(std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "refused")),
kind: "HTTP".to_string(),
url: Some("https://api.example.com".to_string()),
backtrace: Backtrace::generate(),
};
// Configuration errors with suggestions
let config_error = DecrustError::Config {
message: "Invalid database URL format".to_string(),
path: Some("config.toml".into()),
source: OptionalError(None),
backtrace: Backtrace::generate(),
};
Decrust-Core provides the following modules:
backtrace
- Enhanced backtrace capture and analysiscircuit_breaker
- Circuit breaker pattern implementationdecrust
- Core error handling and autocorrection enginereporter
- Error reporting and formattingsyntax
- AST-driven fix generation and templatestypes
- Core type definitions and utilitiesstd-thread
: Enables threading support for circuit breaker timeoutsserde
: Enables serialization support for error typestracing
: Enables integration with the tracing ecosystemdecrust_context_msg()
for better debuggingDecrustError
This project is licensed under the Business Source License 1.1 (BSL 1.1).
Change Date: 2029-05-25 | Change License: GNU GPL v3
Made with ❤️ by ArcMoon Studios