Expand description
Error types Comprehensive error handling framework for P2P Foundation
This module provides a zero-panic error handling system designed to replace 568 unwrap() calls throughout the codebase with proper error propagation and context.
§Features
- Type-safe error hierarchy: Custom error types for all subsystems
- Zero-cost abstractions: Optimized for performance with Cow<’static, str>
- Context propagation: Rich error context without heap allocations
- Structured logging: JSON-based error reporting for production monitoring
- Anyhow integration: Seamless integration for application-level errors
- Recovery patterns: Built-in retry and circuit breaker support
§Usage Examples
§Basic Error Handling
ⓘ
use saorsa_core::error::{P2PError, P2pResult};
use std::net::SocketAddr;
fn connect_to_peer(addr: SocketAddr) -> P2pResult<()> {
// Use proper error propagation instead of unwrap()
// socket.connect(addr).map_err(|e| P2PError::Network(...))?;
Ok(())
}§Adding Context
ⓘ
use saorsa_core::error::{P2PError, P2pResult};
use saorsa_core::error::ErrorContext;
fn load_config(path: &str) -> P2pResult<String> {
std::fs::read_to_string(path)
.context("Failed to read config file")
}§Structured Error Logging
ⓘ
use saorsa_core::error::P2PError;
fn handle_error(err: P2PError) {
// Log with tracing
tracing::error!("Error occurred: {}", err);
}§Migration from unwrap()
ⓘ
use saorsa_core::error::P2PError;
// Before:
// let value = some_operation().unwrap();
// After - use ? operator with proper error types:
// let value = some_operation()?;
// For Option types:
// let value = some_option.ok_or_else(|| P2PError::Internal("Missing value".into()))?;Structs§
- Error
Log - Structured error log entry optimized for performance
- Geographic
Config - Configuration for geographic diversity enforcement
Enums§
- Bootstrap
Error - Bootstrap-related errors
- Config
Error - Configuration-related errors
- Crypto
Error - Cryptography-related errors
- DhtError
- DHT-related errors
- Error
Value - Value types for error context
- GeoEnforcement
Mode - Geographic enforcement mode
- GeoRejection
Error - Geographic validation errors for connection rejection
- Identity
Error - Identity-related errors
- Network
Error - Network-related errors
- P2PError
- Core error type for the P2P Foundation library
- Security
Error - Security-related errors
- Storage
Error - Storage-related errors
- Transport
Error - Transport-related errors
Traits§
- Anyhow
Context - Re-export for convenience
Provides the
contextmethod forResult. - Error
Context - Extension trait for adding context to errors
- Error
Reporting - Error reporting trait for structured logging
- From
Anyhow Ext - Into
Anyhow - Conversion helpers for anyhow integration
- Recoverable
- Trait for errors that can be recovered from with retry
Type Aliases§
- Anyhow
Result - Re-export for convenience
Result<T, Error> - P2pResult
- Result type alias for P2P operations