Skip to main content

zlayer_overlay/
error.rs

1//! Error types for overlay network operations
2
3use std::net::IpAddr;
4use thiserror::Error;
5
6/// Errors that can occur during overlay network operations
7#[derive(Debug, Error)]
8pub enum OverlayError {
9    /// Overlay transport not available
10    #[error("Overlay transport not available: {0}")]
11    TransportNotAvailable(String),
12
13    /// Transport command execution failed
14    #[error("Transport command failed: {0}")]
15    TransportCommand(String),
16
17    /// Boringtun device error
18    #[error("Boringtun device error: {0}")]
19    BoringtunDevice(String),
20
21    /// Invalid CIDR notation
22    #[error("Invalid CIDR notation: {0}")]
23    InvalidCidr(String),
24
25    /// No available IPs in the configured CIDR range
26    #[error("No available IP addresses in CIDR range")]
27    NoAvailableIps,
28
29    /// IP address already allocated
30    #[error("IP address {0} is already allocated")]
31    IpAlreadyAllocated(IpAddr),
32
33    /// IP address not in CIDR range
34    #[error("IP address {0} is not within CIDR range {1}")]
35    IpNotInRange(IpAddr, String),
36
37    /// Interface already exists
38    #[error("Overlay interface '{0}' already exists")]
39    InterfaceExists(String),
40
41    /// Interface not found
42    #[error("Overlay interface '{0}' not found")]
43    InterfaceNotFound(String),
44
45    /// Peer not found
46    #[error("Peer with public key '{0}' not found")]
47    PeerNotFound(String),
48
49    /// Peer is unreachable
50    #[error("Peer at {ip} is unreachable: {reason}")]
51    PeerUnreachable { ip: IpAddr, reason: String },
52
53    /// Configuration file error
54    #[error("Configuration error: {0}")]
55    Config(String),
56
57    /// IO error
58    #[error("IO error: {0}")]
59    Io(#[from] std::io::Error),
60
61    /// JSON serialization/deserialization error
62    #[error("JSON error: {0}")]
63    Json(#[from] serde_json::Error),
64
65    /// Already initialized
66    #[error("Overlay network already initialized at {0}")]
67    AlreadyInitialized(String),
68
69    /// Not initialized
70    #[error("Overlay network not initialized. Run init_leader or join first")]
71    NotInitialized,
72
73    /// Permission denied
74    #[error("Permission denied: {0}. This operation typically requires root privileges")]
75    PermissionDenied(String),
76
77    /// Timeout waiting for operation
78    #[error("Operation timed out: {0}")]
79    Timeout(String),
80
81    /// Invalid key format
82    #[error("Invalid key format: {0}")]
83    InvalidKey(String),
84
85    /// Network configuration error
86    #[error("Network configuration error: {0}")]
87    NetworkConfig(String),
88
89    /// DNS error
90    #[error("DNS error: {0}")]
91    Dns(String),
92
93    /// STUN discovery failed
94    #[error("STUN discovery failed: {0}")]
95    StunDiscovery(String),
96
97    /// Hole punch failed for a specific peer
98    #[error("Hole punch failed for peer {peer}: {reason}")]
99    HolePunchFailed { peer: String, reason: String },
100
101    /// TURN relay error
102    #[error("TURN relay error: {0}")]
103    TurnRelay(String),
104
105    /// NAT traversal exhausted all candidates
106    #[error("NAT traversal failed for peer {peer}: exhausted all candidates")]
107    NatTraversalFailed { peer: String },
108
109    /// No STUN servers configured
110    #[error("No STUN servers configured")]
111    NoStunServers,
112}
113
114/// Result type alias for overlay operations
115pub type Result<T> = std::result::Result<T, OverlayError>;