Skip to main content

Module error

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

ErrorLog
Structured error log entry optimized for performance
GeographicConfig
Configuration for geographic diversity enforcement

Enums§

BootstrapError
Bootstrap-related errors
ConfigError
Configuration-related errors
CryptoError
Cryptography-related errors
DhtError
DHT-related errors
ErrorValue
Value types for error context
GeoEnforcementMode
Geographic enforcement mode
GeoRejectionError
Geographic validation errors for connection rejection
IdentityError
Identity-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