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 p2p_core::error::{P2PError, P2pResult, ErrorContext};
fn connect_to_peer(addr: SocketAddr) -> P2pResult<Connection> {
// Instead of: socket.connect(addr).unwrap()
let conn = socket.connect(addr)
.map_err(|e| NetworkError::ConnectionFailed {
addr,
reason: e.to_string().into(),
})?;
Ok(conn)
}§Adding Context
use p2p_core::error::{P2PError, P2pResult, ErrorContext};
fn load_config(path: &str) -> P2pResult<Config> {
std::fs::read_to_string(path)
.context("Failed to read config file")?
.parse()
.context("Failed to parse config")
}§Structured Error Logging
use p2p_core::error::{P2PError, ErrorReporting};
fn handle_error(err: P2PError) {
// Automatically logs with appropriate level and context
err.log();
// Or get structured log for custom handling
let log_entry = err.to_error_log();
send_to_monitoring_system(&log_entry);
}§Migration from unwrap()
// Before:
let value = some_operation().unwrap();
// After:
let value = some_operation()
.context("Failed to perform operation")?;
// For performance-critical paths with known bounds:
let value = some_operation()
.ok_or_else(|| P2PError::Internal("Operation failed".into()))?;Structs§
- Error
Log - Structured error log entry optimized for performance
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
- 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