Runtime Error
Error Code: RUNTIME_ERROR
This error occurs when an unexpected runtime issue happens during FerricLink operations.
Common Causes
- Memory Issues: Out of memory or memory allocation failures
- Thread Panics: Panics in async or threaded operations
- Resource Exhaustion: File handles, network connections, etc.
- Unexpected State: Invalid state transitions or conditions
Solutions
1. Memory Management
use ferriclink_core::FerricLinkError;
fn process_large_data(data: &[u8]) -> Result<Vec<u8>, FerricLinkError> {
// Process data in chunks to avoid memory issues
const CHUNK_SIZE: usize = 1024 * 1024; // 1MB chunks
let mut result = Vec::new();
for chunk in data.chunks(CHUNK_SIZE) {
let processed = process_chunk(chunk)?;
result.extend_from_slice(&processed);
}
Ok(result)
}
fn process_chunk(chunk: &[u8]) -> Result<Vec<u8>, FerricLinkError> {
// Process individual chunk
Ok(chunk.to_vec())
}
2. Error Recovery
use ferriclink_core::FerricLinkError;
async fn robust_operation() -> Result<String, FerricLinkError> {
// Try the operation with timeout
let result = tokio::time::timeout(
Duration::from_secs(30),
perform_operation()
).await
.map_err(|_| FerricLinkError::runtime("Operation timed out"))?;
result
}
async fn perform_operation() -> Result<String, FerricLinkError> {
// Your operation here
Ok("Success".to_string())
}
3. Resource Cleanup
use ferriclink_core::FerricLinkError;
use std::sync::Arc;
struct ResourceManager {
resources: Arc<Mutex<Vec<Resource>>>,
}
impl ResourceManager {
fn new() -> Self {
Self {
resources: Arc::new(Mutex::new(Vec::new())),
}
}
async fn cleanup(&self) -> Result<(), FerricLinkError> {
let mut resources = self.resources.lock().await;
for resource in resources.drain(..) {
resource.cleanup().await
.map_err(|e| FerricLinkError::runtime(format!("Cleanup failed: {}", e)))?;
}
Ok(())
}
}
impl Drop for ResourceManager {
fn drop(&mut self) {
// Ensure cleanup happens even on panic
if let Ok(rt) = tokio::runtime::Handle::try_current() {
rt.block_on(self.cleanup()).ok();
}
}
}
Prevention
- Implement proper resource management
- Use timeouts for operations
- Handle panics gracefully
- Monitor memory usage
- Implement proper cleanup
- Use structured error handling