-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy patherror.rs
More file actions
234 lines (195 loc) · 6.9 KB
/
error.rs
File metadata and controls
234 lines (195 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use thiserror::Error;
use crate::core::ovpn_parser::error::OvpnParseError;
use super::connection_state::ConnectionStateReason;
use super::state_reason::StateReason;
/// Errors that can occur during network operations.
///
/// This enum provides specific error types for different failure modes,
/// making it easy to handle errors appropriately in your application.
///
/// # Examples
///
/// ## Basic Error Handling
///
/// ```no_run
/// use nmrs::{NetworkManager, WifiSecurity, ConnectionError};
///
/// # async fn example() -> nmrs::Result<()> {
/// let nm = NetworkManager::new().await?;
///
/// match nm.connect("MyNetwork", None, WifiSecurity::WpaPsk {
/// psk: "password".into()
/// }).await {
/// Ok(_) => println!("Connected!"),
/// Err(ConnectionError::AuthFailed) => {
/// eprintln!("Wrong password");
/// }
/// Err(ConnectionError::NotFound) => {
/// eprintln!("Network not in range");
/// }
/// Err(ConnectionError::Timeout) => {
/// eprintln!("Connection timed out");
/// }
/// Err(e) => eprintln!("Error: {}", e),
/// }
/// # Ok(())
/// # }
/// ```
///
/// ## Retry Logic
///
/// ```no_run
/// use nmrs::{NetworkManager, WifiSecurity, ConnectionError};
///
/// # async fn example() -> nmrs::Result<()> {
/// let nm = NetworkManager::new().await?;
///
/// for attempt in 1..=3 {
/// match nm.connect("MyNetwork", None, WifiSecurity::Open).await {
/// Ok(_) => {
/// println!("Connected on attempt {}", attempt);
/// break;
/// }
/// Err(ConnectionError::Timeout) if attempt < 3 => {
/// println!("Timeout, retrying...");
/// continue;
/// }
/// Err(e) => return Err(e),
/// }
/// }
/// # Ok(())
/// # }
/// ```
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum ConnectionError {
/// A D-Bus communication error occurred.
#[error("D-Bus error: {0}")]
Dbus(#[from] zbus::Error),
/// The requested network was not found during scan.
#[error("network not found")]
NotFound,
/// Authentication with the access point failed (wrong password, rejected credentials).
#[error("authentication failed")]
AuthFailed,
/// The supplicant (wpa_supplicant) encountered a configuration error.
#[error("supplicant configuration failed")]
SupplicantConfigFailed,
/// The supplicant timed out during authentication.
#[error("supplicant timeout")]
SupplicantTimeout,
/// DHCP failed to obtain an IP address.
#[error("DHCP failed")]
DhcpFailed,
/// The connection timed out waiting for activation.
#[error("connection timeout")]
Timeout,
/// The connection is stuck in an unexpected state.
#[error("connection stuck in state: {0}")]
Stuck(String),
/// No Wi-Fi device was found on the system.
#[error("no Wi-Fi device found")]
NoWifiDevice,
/// No wired (ethernet) device was found on the system.
#[error("no wired device was found")]
NoWiredDevice,
/// Wi-Fi device did not become ready in time.
#[error("Wi-Fi device not ready")]
WifiNotReady,
/// No saved connection exists for the requested network.
#[error("no saved connection for network")]
NoSavedConnection,
/// No saved profile with the given UUID.
#[error("saved connection '{0}' not found")]
SavedConnectionNotFound(String),
/// Saved profile settings are missing required keys or are inconsistent.
#[error("saved connection malformed: {0}")]
MalformedSavedConnection(String),
/// An empty password was provided for the requested network.
#[error("no password was provided")]
MissingPassword,
/// A general connection failure with a device state reason code.
#[error("connection failed: {0}")]
DeviceFailed(StateReason),
/// A connection activation failure with a connection state reason.
#[error("connection activation failed: {0}")]
ActivationFailed(ConnectionStateReason),
/// Invalid UTF-8 encountered in SSID.
#[error("invalid UTF-8 in SSID: {0}")]
InvalidUtf8(#[from] std::str::Utf8Error),
/// No VPN connection found
#[error("no VPN connection found")]
NoVpnConnection,
/// Invalid IP address or CIDR notation
#[error("invalid address: {0}")]
InvalidAddress(String),
/// Invalid VPN peer configuration
#[error("invalid peer configuration: {0}")]
InvalidPeers(String),
/// Invalid WireGuard private key format
#[error("invalid WireGuard private key: {0}")]
InvalidPrivateKey(String),
/// Invalid WireGuard public key format
#[error("invalid WireGuard public key: {0}")]
InvalidPublicKey(String),
/// Invalid VPN gateway format (should be host:port)
#[error("invalid VPN gateway: {0}")]
InvalidGateway(String),
/// VPN connection failed
#[error("VPN connection failed: {0}")]
VpnFailed(String),
/// Bluetooth device not found
#[error("Bluetooth device not found")]
NoBluetoothDevice,
/// A D-Bus operation failed with context about what was being attempted
#[error("{context}: {source}")]
DbusOperation {
context: String,
#[source]
source: zbus::Error,
},
/// Secret agent registration with NetworkManager failed.
#[error("secret agent registration failed: {context}")]
AgentRegistration {
/// What went wrong during registration.
context: String,
},
/// Operation requires a registered secret agent but none is active.
#[error("secret agent not registered")]
AgentNotRegistered,
/// A secret agent is already registered under this identifier.
#[error("secret agent already registered under this identifier")]
AgentAlreadyRegistered,
/// An error occured while parsing a configuration
#[error("error while parsing a configuration: {0}")]
ParseError(OvpnParseError),
/// Access point with the given SSID and BSSID was not found.
#[error("access point for SSID '{ssid}' with BSSID '{bssid}' not found")]
ApBssidNotFound {
/// SSID that was searched for.
ssid: String,
/// BSSID that was searched for.
bssid: String,
},
/// Invalid BSSID format.
#[error("invalid BSSID format: '{0}' (expected XX:XX:XX:XX:XX:XX)")]
InvalidBssid(String),
/// Interface exists but is not a Wi-Fi device.
#[error("interface '{interface}' is not a Wi-Fi device")]
NotAWifiDevice {
/// The interface name that was checked.
interface: String,
},
/// No Wi-Fi device with the given interface name.
#[error("no Wi-Fi device named '{interface}'")]
WifiInterfaceNotFound {
/// The interface name that was searched for.
interface: String,
},
/// A radio is hardware-disabled via rfkill.
#[error("radio is hardware-disabled (rfkill)")]
HardwareRadioKilled,
/// The BlueZ Bluetooth stack is unavailable.
#[error("bluetooth stack unavailable: {0}")]
BluezUnavailable(String),
}