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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! Error types for vanguards-rs.
//!
//! This module defines the [`enum@Error`] enum representing all possible error conditions
//! in the vanguards-rs library. Each variant provides specific information about
//! the failure and guidance on recovery.
//!
//! # Overview
//!
//! The error system is designed to provide:
//!
//! - **Specific error types** for different failure categories
//! - **Recovery guidance** for each error type
//! - **Seamless integration** with stem-rs errors
//! - **Informative messages** without leaking sensitive data
//!
//! # Error Categories
//!
//! ```text
//! Error
//! ├── Io ◄── File/network I/O failures
//! ├── Config ◄── Invalid configuration
//! ├── Control ◄── Tor control protocol errors (from stem-rs)
//! ├── State ◄── State file corruption/format issues
//! ├── Consensus ◄── Consensus parsing failures
//! ├── NoNodesRemain ◄── All relays filtered out
//! ├── Validation ◄── Invalid input data
//! └── DescriptorUnavailable ◄── Missing descriptors
//! ```
//!
//! # Recovery Guide
//!
//! | Error | Recoverable | Retry | Recommended Action |
//! |-------|-------------|-------|-------------------|
//! | [`Io`](Error::Io) | Sometimes | Yes (backoff) | Check permissions, disk space |
//! | [`Config`](Error::Config) | No | No | Fix configuration file |
//! | [`Control`](Error::Control) | Sometimes | Yes | Reconnect to Tor |
//! | [`State`](Error::State) | Sometimes | No | Delete state file, restart |
//! | [`Consensus`](Error::Consensus) | Sometimes | Yes | Wait for new consensus |
//! | [`NoNodesRemain`](Error::NoNodesRemain) | No | No | Adjust ExcludeNodes |
//! | [`Validation`](Error::Validation) | No | No | Fix input data |
//! | [`DescriptorUnavailable`](Error::DescriptorUnavailable) | Yes | Yes | Wait for bootstrap |
//!
//! # Example
//!
//! ## Basic Error Handling
//!
//! ```rust
//! use vanguards_rs::{Config, Error, Result};
//!
//! fn load_config() -> Result<Config> {
//! let config = Config::from_file(std::path::Path::new("vanguards.conf"))?;
//! config.validate()?;
//! Ok(config)
//! }
//!
//! fn main() {
//! match load_config() {
//! Ok(config) => println!("Config loaded successfully"),
//! Err(Error::Io(e)) => eprintln!("File error: {}", e),
//! Err(Error::Config(msg)) => eprintln!("Config error: {}", msg),
//! Err(e) => eprintln!("Other error: {}", e),
//! }
//! }
//! ```
//!
//! ## Retry Logic
//!
//! ```rust,no_run
//! use vanguards_rs::{Error, Result};
//! use std::time::Duration;
//!
//! async fn with_retry<F, T>(mut f: F, max_retries: u32) -> Result<T>
//! where
//! F: FnMut() -> Result<T>,
//! {
//! let mut attempts = 0;
//! loop {
//! match f() {
//! Ok(result) => return Ok(result),
//! Err(Error::Io(_)) | Err(Error::Control(_)) if attempts < max_retries => {
//! attempts += 1;
//! tokio::time::sleep(Duration::from_secs(1 << attempts)).await;
//! }
//! Err(e) => return Err(e),
//! }
//! }
//! }
//! ```
//!
//! # See Also
//!
//! - [`Result`] - Type alias for `std::result::Result<T, Error>`
//! - [`stem_rs::Error`] - Underlying Tor control errors
//! - [`Config::validate`](crate::Config::validate) - Configuration validation
use Error;
/// Errors that can occur during vanguards-rs operations.
///
/// This enum represents all possible error conditions in the library.
/// Each variant provides specific information about the failure and
/// guidance on recovery.
///
/// # Error Handling Patterns
///
/// ## Match on Specific Errors
///
/// ```rust
/// use vanguards_rs::Error;
///
/// fn handle_error(err: Error) {
/// match err {
/// Error::Io(io_err) => {
/// eprintln!("I/O error: {}", io_err);
/// // Check file permissions, disk space, network
/// }
/// Error::Config(msg) => {
/// eprintln!("Configuration error: {}", msg);
/// // Fix configuration and restart
/// }
/// Error::Control(ctrl_err) => {
/// eprintln!("Tor control error: {}", ctrl_err);
/// // Reconnect to Tor
/// }
/// Error::State(msg) => {
/// eprintln!("State file error: {}", msg);
/// // Delete state file and restart
/// }
/// Error::Consensus(msg) => {
/// eprintln!("Consensus error: {}", msg);
/// // Wait for Tor to get new consensus
/// }
/// Error::NoNodesRemain => {
/// eprintln!("No nodes remain after filtering");
/// // Adjust ExcludeNodes configuration
/// }
/// Error::Validation(msg) => {
/// eprintln!("Validation error: {}", msg);
/// // Fix invalid input
/// }
/// Error::DescriptorUnavailable(msg) => {
/// eprintln!("Descriptor unavailable: {}", msg);
/// // Wait for Tor to finish bootstrapping
/// }
/// }
/// }
/// ```
///
/// ## Check if Retryable
///
/// ```rust
/// use vanguards_rs::Error;
///
/// fn is_retryable(err: &Error) -> bool {
/// matches!(err,
/// Error::Io(_) |
/// Error::Control(_) |
/// Error::Consensus(_) |
/// Error::DescriptorUnavailable(_)
/// )
/// }
/// ```
///
/// # See Also
///
/// - [`Result`] - Type alias using this error type
/// - [`stem_rs::Error`] - Underlying control protocol errors
/// Result type alias for vanguards-rs operations.
///
/// This is a convenience alias for `std::result::Result<T, Error>` used
/// throughout the vanguards-rs library.
///
/// # Example
///
/// ```rust
/// use vanguards_rs::{Config, Result};
///
/// fn load_and_validate_config() -> Result<Config> {
/// let config = Config::from_file(std::path::Path::new("vanguards.conf"))?;
/// config.validate()?;
/// Ok(config)
/// }
/// ```
///
/// # See Also
///
/// - [`enum@Error`] - The error type used in this result
pub type Result<T> = Result;