Module error

Source
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§

ErrorLog
Structured error log entry optimized for performance

Enums§

BootstrapError
Bootstrap-related errors
ConfigError
Configuration-related errors
CryptoError
Cryptography-related errors
DhtError
DHT-related errors
ErrorValue
Value types for error context
IdentityError
Identity-related errors
McpError
MCP-related errors
NetworkError
Network-related errors
P2PError
Core error type for the P2P Foundation library
SecurityError
Security-related errors
StorageError
Storage-related errors
TransportError
Transport-related errors

Traits§

AnyhowContext
Re-export for convenience Provides the context method for Result.
ErrorContext
Extension trait for adding context to errors
ErrorReporting
Error reporting trait for structured logging
FromAnyhowExt
IntoAnyhow
Conversion helpers for anyhow integration
Recoverable
Trait for errors that can be recovered from with retry

Type Aliases§

AnyhowResult
Re-export for convenience Result<T, Error>
P2pResult
Result type alias for P2P operations