Expand description
Advanced error handling and recovery system for SciRS2
This module provides a comprehensive error handling framework including:
- Core error types and context management
- Advanced recovery strategies with retry mechanisms
- Circuit breaker patterns for fault tolerance
- Async error handling with timeout and progress tracking
- Comprehensive error diagnostics and pattern analysis
- Environment-aware error analysis and suggestions
§Quick Start
use scirs2_core::error::{CoreError, CoreResult, RecoverableError};
use scirs2_core::error_context;
fn example_operation() -> CoreResult<i32> {
// Basic error with context
if false {
return Err(CoreError::ComputationError(
error_context!("Operation failed")
));
}
Ok(42)
}
// Enhanced error with recovery suggestions
let error = CoreError::MemoryError(error_context!("Out of memory"));
let recoverable = RecoverableError::error(error);
println!("{}", recoverable.recovery_report());
§Advanced Features
§Retry Mechanisms
use scirs2_core::error::recovery::{RetryExecutor, RecoveryStrategy};
use std::time::Duration;
let executor = RetryExecutor::new(RecoveryStrategy::ExponentialBackoff {
max_attempts: 3,
initialdelay: Duration::from_millis(100),
maxdelay: Duration::from_secs(5),
multiplier: 2.0,
});
let result = executor.execute(|| {
// Your operation here
Ok(42)
});
§Circuit Breaker Pattern
use scirs2_core::error::recovery::CircuitBreaker;
use std::time::Duration;
let circuitbreaker = CircuitBreaker::new(
5, // failure threshold
Duration::from_secs(30), // timeout
Duration::from_secs(60), // recovery timeout
);
let result = circuitbreaker.execute(|| {
// Your operation here
Ok(42)
});
§Async Error Handling
use scirs2_core::error::recovery::{RecoveryStrategy, RetryExecutor};
use std::time::Duration;
// Synchronous retry example
let executor = RetryExecutor::new(
RecoveryStrategy::LinearBackoff {
max_attempts: 3,
delay: Duration::from_millis(100),
}
);
let mut counter = 0;
let retry_result = executor.execute(|| {
counter += 1;
if counter < 3 {
Err(scirs2_core::error::CoreError::ComputationError(
scirs2_core::error::ErrorContext::new("Temporary failure".to_string())
))
} else {
Ok::<i32, scirs2_core::error::CoreError>(123)
}
});
assert!(retry_result.is_ok());
§Error Diagnostics
use scirs2_core::error::diagnostics::error;
use scirs2_core::error::{CoreError, ErrorContext, ErrorLocation};
let err = CoreError::ConvergenceError(
ErrorContext::new("Failed to converge")
.with_location(ErrorLocation::new(file!(), line!()))
);
let diagnostics = error(&err);
println!("{}", diagnostics); // Comprehensive diagnostic report
Re-exports§
pub use recovery::hints;
pub use recovery::ErrorAggregator;
pub use recovery::ErrorSeverity;
pub use recovery::RecoverableError;
pub use recovery::RecoveryHint;
pub use recovery::RecoveryStrategy;
pub use diagnostics::error;
pub use diagnostics::error_with_context;
pub use diagnostics::EnvironmentInfo;
pub use diagnostics::ErrorDiagnosticReport;
pub use diagnostics::ErrorDiagnostics;
pub use diagnostics::ErrorOccurrence;
pub use diagnostics::ErrorPattern;
pub use diagnostics::PerformanceImpact;
pub use circuitbreaker::get_circuitbreaker;
pub use circuitbreaker::list_circuitbreakers;
pub use circuitbreaker::CircuitBreaker;
pub use circuitbreaker::CircuitBreakerConfig;
pub use circuitbreaker::CircuitBreakerStatus;
pub use circuitbreaker::CircuitState;
pub use circuitbreaker::FallbackStrategy;
pub use circuitbreaker::ResilientExecutor;
pub use circuitbreaker::RetryExecutor;
pub use circuitbreaker::RetryPolicy;
Modules§
- circuitbreaker
- Circuit breaker pattern for error handling
- diagnostics
- Advanced error diagnostics and reporting for
SciRS2
- prelude
- Prelude module for convenient imports Commonly used error handling types and functions
- recovery
- Advanced error recovery and resilience mechanisms for
SciRS2
Structs§
- Error
Context - Error context containing additional information about an error
- Error
Location - Location information for error context
Enums§
- Core
Error - Core error type for
SciRS2
Functions§
- chainerror
- Create an error chain by adding a new error context
- check_
dimensions - Checks dimensions
- check_
domain - Checks if a condition is true, otherwise returns a domain error
- check_
value - Checks if a value is valid
- context
- Get predictive error analysis for a given context
- converterror
- Convert an error from one type to a CoreError
- diagnoseerror
- Beta 2 Enhanced Diagnostic Functions
- diagnoseerror_
advanced - Advanced error diagnosis function.
- get_
recovery_ strategies - Get domain-specific recovery strategies for an error
- recorderror
- Record an error for pattern analysis
- validate
- Checks if a value is valid according to a validator function
Type Aliases§
- Core
Result - Result type alias for core operations