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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//! Typed error handling for hypertor
//!
//! All errors are typed using `thiserror` for:
//! - Compile-time error checking
//! - Pattern matching on error variants
//! - Python exception mapping
//! - Zero panics guarantee
use std::time::Duration;
use thiserror::Error;
/// Result type alias for hypertor operations
pub type Result<T> = std::result::Result<T, Error>;
/// All errors that can occur in hypertor
#[derive(Error, Debug)]
pub enum Error {
// =========================================================================
// Tor Network Errors
// =========================================================================
/// Failed to bootstrap the Tor client
#[error("Tor bootstrap failed: {message}")]
Bootstrap {
/// Human-readable error message
message: String,
/// Underlying arti error
#[source]
source: Option<BoxedError>,
},
/// Failed to establish connection through Tor
#[error("Connection to {host}:{port} failed")]
Connection {
/// Target hostname
host: String,
/// Target port
port: u16,
/// Underlying error
#[source]
source: BoxedError,
},
/// Circuit creation failed
#[error("Failed to create Tor circuit: {message}")]
Circuit {
/// Error details
message: String,
/// Underlying error
#[source]
source: Option<BoxedError>,
},
// =========================================================================
// TLS Errors
// =========================================================================
/// TLS handshake failed
#[error("TLS handshake failed for {host}")]
TlsHandshake {
/// Target hostname
host: String,
/// Underlying TLS error
#[source]
source: BoxedError,
},
/// TLS configuration error
#[error("TLS configuration error: {message}")]
TlsConfig {
/// Error details
message: String,
},
/// Certificate verification failed
#[error("Certificate verification failed for {host}: {reason}")]
CertificateVerification {
/// Target hostname
host: String,
/// Reason for failure
reason: String,
},
// =========================================================================
// HTTP Errors
// =========================================================================
/// HTTP protocol error
#[error("HTTP error: {message}")]
Http {
/// Error details
message: String,
/// Underlying hyper error
#[source]
source: Option<BoxedError>,
},
/// Invalid HTTP request
#[error("Invalid request: {message}")]
InvalidRequest {
/// What's wrong with the request
message: String,
},
/// Response body too large
#[error("Response too large: {size} bytes exceeds limit of {limit} bytes")]
ResponseTooLarge {
/// Actual response size
size: usize,
/// Configured limit
limit: usize,
},
/// Invalid URL
#[error("Invalid URL: {url}")]
InvalidUrl {
/// The invalid URL
url: String,
/// What's wrong with it
reason: String,
},
/// Missing hostname in URL
#[error("URL missing hostname")]
MissingHost,
/// Too many redirects
#[error("Too many redirects: {count} (limit: {limit})")]
TooManyRedirects {
/// Number of redirects followed
count: u32,
/// Maximum allowed
limit: u32,
},
// =========================================================================
// Timeout Errors
// =========================================================================
/// Operation timed out
#[error("{operation} timed out after {duration:?}")]
Timeout {
/// What operation timed out
operation: String,
/// How long we waited
duration: Duration,
},
/// Connection pool exhausted
#[error("Connection pool exhausted, max {max_connections} connections")]
PoolExhausted {
/// Maximum configured connections
max_connections: usize,
},
// =========================================================================
// I/O Errors
// =========================================================================
/// Generic I/O error
#[error("I/O error: {message}")]
Io {
/// Error context
message: String,
/// Underlying I/O error
#[source]
source: std::io::Error,
},
// =========================================================================
// Configuration Errors
// =========================================================================
/// Invalid configuration
#[error("Configuration error: {message}")]
Config {
/// What's wrong with the configuration
message: String,
},
/// Protocol error (SOCKS5, etc.)
#[error("Protocol error: {0}")]
Protocol(String),
}
/// Boxed error for storing heterogeneous error sources
pub type BoxedError = Box<dyn std::error::Error + Send + Sync + 'static>;
impl Error {
/// Create a bootstrap error
pub fn bootstrap(message: impl Into<String>) -> Self {
Self::Bootstrap {
message: message.into(),
source: None,
}
}
/// Create a bootstrap error with source
pub fn bootstrap_with_source(
message: impl Into<String>,
source: impl std::error::Error + Send + Sync + 'static,
) -> Self {
Self::Bootstrap {
message: message.into(),
source: Some(Box::new(source)),
}
}
/// Create a connection error
pub fn connection(
host: impl Into<String>,
port: u16,
source: impl std::error::Error + Send + Sync + 'static,
) -> Self {
Self::Connection {
host: host.into(),
port,
source: Box::new(source),
}
}
/// Create an HTTP error
pub fn http(message: impl Into<String>) -> Self {
Self::Http {
message: message.into(),
source: None,
}
}
/// Create an HTTP error with source
pub fn http_with_source(
message: impl Into<String>,
source: impl std::error::Error + Send + Sync + 'static,
) -> Self {
Self::Http {
message: message.into(),
source: Some(Box::new(source)),
}
}
/// Create a TLS handshake error
pub fn tls_handshake(
host: impl Into<String>,
source: impl std::error::Error + Send + Sync + 'static,
) -> Self {
Self::TlsHandshake {
host: host.into(),
source: Box::new(source),
}
}
/// Create a timeout error
pub fn timeout(operation: impl Into<String>, duration: Duration) -> Self {
Self::Timeout {
operation: operation.into(),
duration,
}
}
/// Create an invalid URL error
pub fn invalid_url(url: impl Into<String>, reason: impl Into<String>) -> Self {
Self::InvalidUrl {
url: url.into(),
reason: reason.into(),
}
}
/// Create an I/O error
pub fn io(message: impl Into<String>, source: std::io::Error) -> Self {
Self::Io {
message: message.into(),
source,
}
}
/// Create a config error
pub fn config(message: impl Into<String>) -> Self {
Self::Config {
message: message.into(),
}
}
/// Returns true if this error is retryable
pub fn is_retryable(&self) -> bool {
matches!(
self,
Error::Connection { .. }
| Error::Circuit { .. }
| Error::Timeout { .. }
| Error::PoolExhausted { .. }
)
}
/// Returns true if this is a timeout error
pub fn is_timeout(&self) -> bool {
matches!(self, Error::Timeout { .. })
}
/// Returns true if this is a TLS-related error
pub fn is_tls(&self) -> bool {
matches!(
self,
Error::TlsHandshake { .. }
| Error::TlsConfig { .. }
| Error::CertificateVerification { .. }
)
}
}
// Conversions from common error types
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Self::Io {
message: err.to_string(),
source: err,
}
}
}
impl From<http::uri::InvalidUri> for Error {
fn from(err: http::uri::InvalidUri) -> Self {
Self::InvalidUrl {
url: String::new(),
reason: err.to_string(),
}
}
}
impl From<http::Error> for Error {
fn from(err: http::Error) -> Self {
Self::Http {
message: err.to_string(),
source: Some(Box::new(err)),
}
}
}
impl From<hyper::Error> for Error {
fn from(err: hyper::Error) -> Self {
Self::Http {
message: err.to_string(),
source: Some(Box::new(err)),
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used)]
use super::*;
#[test]
fn test_error_is_retryable() {
let timeout = Error::timeout("request", Duration::from_secs(30));
assert!(timeout.is_retryable());
let config = Error::config("bad config");
assert!(!config.is_retryable());
}
#[test]
fn test_error_display() {
let err = Error::timeout("connection", Duration::from_secs(10));
assert_eq!(err.to_string(), "connection timed out after 10s");
}
}