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
use std::sync::Arc;
use thiserror::Error;
use tor_error::{ErrorKind, HasKind};
use tor_linkspec::OwnedChanTarget;
use tor_rtcompat::TimeoutError;
use crate::SourceInfo;
#[derive(Error, Debug, Clone)]
#[non_exhaustive]
pub enum Error {
#[error("Error while getting a circuit {0}")]
CircMgr(#[from] tor_circmgr::Error),
#[error("Error fetching directory information from {source:?}")]
RequestFailed {
source: Option<SourceInfo>,
#[source]
error: RequestError,
},
}
#[derive(Error, Debug, Clone)]
#[non_exhaustive]
pub enum RequestError {
#[error("directory timed out")]
DirTimeout,
#[error("truncated HTTP headers")]
TruncatedHeaders,
#[error("response too long; gave up after {0} bytes")]
ResponseTooLong(usize),
#[error("Couldn't decode data as UTF-8.")]
Utf8Encoding(#[from] std::string::FromUtf8Error),
#[error("IO error: {0}")]
IoError(#[source] Arc<std::io::Error>),
#[error("Protocol error while launching a stream: {0}")]
Proto(#[from] tor_proto::Error),
#[error("Couldn't parse HTTP headers")]
HttparseError(#[from] httparse::Error),
#[error("Couldn't create HTTP request")]
HttpError(#[source] Arc<http::Error>),
#[error("Unrecognized content encoding: {0:?}")]
ContentEncoding(String),
#[error("Too much clock skew with directory cache")]
TooMuchClockSkew,
#[error("The requested SHA256 digest of microdescriptors is empty")]
MdSha256Empty,
}
impl From<TimeoutError> for RequestError {
fn from(_: TimeoutError) -> Self {
RequestError::DirTimeout
}
}
impl From<std::io::Error> for RequestError {
fn from(err: std::io::Error) -> Self {
Self::IoError(Arc::new(err))
}
}
impl From<http::Error> for RequestError {
fn from(err: http::Error) -> Self {
Self::HttpError(Arc::new(err))
}
}
impl Error {
pub fn should_retire_circ(&self) -> bool {
match self {
Error::CircMgr(_) => true,
Error::RequestFailed { error, .. } => error.should_retire_circ(),
}
}
pub fn cache_ids(&self) -> Vec<&OwnedChanTarget> {
match &self {
Error::CircMgr(e) => e.peers(),
Error::RequestFailed {
source: Some(source),
..
} => vec![source.cache_id()],
_ => Vec::new(),
}
}
}
impl RequestError {
pub fn should_retire_circ(&self) -> bool {
true
}
}
impl HasKind for RequestError {
fn kind(&self) -> ErrorKind {
use ErrorKind as EK;
use RequestError as E;
match self {
E::DirTimeout => EK::TorNetworkTimeout,
E::TruncatedHeaders => EK::TorProtocolViolation,
E::ResponseTooLong(_) => EK::TorProtocolViolation,
E::Utf8Encoding(_) => EK::TorProtocolViolation,
E::IoError(_) => EK::TorDirectoryError,
E::Proto(e) => e.kind(),
E::HttparseError(_) => EK::TorProtocolViolation,
E::HttpError(_) => EK::Internal,
E::ContentEncoding(_) => EK::TorProtocolViolation,
E::TooMuchClockSkew => EK::TorDirectoryError,
E::MdSha256Empty => EK::Internal,
}
}
}
impl HasKind for Error {
fn kind(&self) -> ErrorKind {
use Error as E;
match self {
E::CircMgr(e) => e.kind(),
E::RequestFailed { error, .. } => error.kind(),
}
}
}