1use std::sync::Arc;
4
5use thiserror::Error;
6use tor_error::{Bug, ErrorKind, HasKind};
7use tor_linkspec::OwnedChanTarget;
8use tor_rtcompat::TimeoutError;
9
10use crate::SourceInfo;
11
12#[derive(Error, Debug, Clone)]
14#[non_exhaustive]
15#[allow(clippy::large_enum_variant)] pub enum Error {
17 #[error("Error while getting a circuit")]
19 CircMgr(#[from] tor_circmgr::Error),
20
21 #[error("Error fetching directory information")]
23 RequestFailed(#[from] RequestFailedError),
24
25 #[error("Internal error")]
27 Bug(#[from] Bug),
28}
29
30#[derive(Error, Debug, Clone)]
32#[allow(clippy::exhaustive_structs)] #[error("Request failed{}", FromSource(.source))]
34pub struct RequestFailedError {
35 pub source: Option<SourceInfo>,
37
38 #[source]
40 pub error: RequestError,
41}
42
43struct FromSource<'a>(&'a Option<SourceInfo>);
45
46impl std::fmt::Display for FromSource<'_> {
47 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48 if let Some(si) = self.0 {
49 write!(f, " from {}", si)
50 } else {
51 Ok(())
52 }
53 }
54}
55
56#[derive(Error, Debug, Clone)]
58#[non_exhaustive]
59pub enum RequestError {
60 #[error("directory timed out")]
62 DirTimeout,
63
64 #[error("truncated HTTP headers")]
66 TruncatedHeaders,
67
68 #[error("response too long; gave up after {0} bytes")]
70 ResponseTooLong(usize),
71
72 #[error("headers too long; gave up after {0} bytes")]
74 HeadersTooLong(usize),
75
76 #[error("Couldn't decode data as UTF-8.")]
78 Utf8Encoding(#[from] std::string::FromUtf8Error),
79
80 #[error("IO error")]
82 IoError(#[source] Arc<std::io::Error>),
83
84 #[error("Protocol error while launching a stream")]
86 Proto(#[from] tor_proto::Error),
87
88 #[error("Tunnel error")]
90 Tunnel(#[from] tor_circmgr::Error),
91
92 #[error("Couldn't parse HTTP headers")]
94 HttparseError(#[from] httparse::Error),
95
96 #[error("Couldn't create HTTP request")]
102 HttpError(#[source] Arc<http::Error>),
103
104 #[error("Unrecognized content encoding: {0:?}")]
106 ContentEncoding(String),
107
108 #[error("Too much clock skew with directory cache")]
113 TooMuchClockSkew,
114
115 #[error("We didn't have any objects to request")]
120 EmptyRequest,
121
122 #[error("HTTP status code {0}: {1:?}")]
124 HttpStatus(u16, String),
125}
126
127impl From<TimeoutError> for RequestError {
128 fn from(_: TimeoutError) -> Self {
129 RequestError::DirTimeout
130 }
131}
132
133impl From<std::io::Error> for RequestError {
134 fn from(err: std::io::Error) -> Self {
135 Self::IoError(Arc::new(err))
136 }
137}
138
139impl From<http::Error> for RequestError {
140 fn from(err: http::Error) -> Self {
141 Self::HttpError(Arc::new(err))
142 }
143}
144
145impl Error {
146 pub fn should_retire_circ(&self) -> bool {
149 match self {
152 Error::CircMgr(_) => true, Error::RequestFailed(RequestFailedError { error, .. }) => error.should_retire_circ(),
154 Error::Bug(_) => true,
155 }
156 }
157
158 pub fn cache_ids(&self) -> Vec<&OwnedChanTarget> {
163 match &self {
164 Error::CircMgr(e) => e.peers(),
165 Error::RequestFailed(RequestFailedError {
166 source: Some(source),
167 ..
168 }) => vec![source.cache_id()],
169 _ => Vec::new(),
170 }
171 }
172}
173
174impl RequestError {
175 pub fn should_retire_circ(&self) -> bool {
178 true
181 }
182}
183
184impl HasKind for RequestError {
185 fn kind(&self) -> ErrorKind {
186 use ErrorKind as EK;
187 use RequestError as E;
188 match self {
189 E::DirTimeout => EK::TorNetworkTimeout,
190 E::TruncatedHeaders => EK::TorProtocolViolation,
191 E::ResponseTooLong(_) => EK::TorProtocolViolation,
192 E::HeadersTooLong(_) => EK::TorProtocolViolation,
193 E::Utf8Encoding(_) => EK::TorProtocolViolation,
194 E::IoError(_) => EK::TorDirectoryError,
198 E::Proto(e) => e.kind(),
199 E::HttparseError(_) => EK::TorProtocolViolation,
200 E::HttpError(_) => EK::Internal,
201 E::ContentEncoding(_) => EK::TorProtocolViolation,
202 E::TooMuchClockSkew => EK::TorDirectoryError,
203 E::EmptyRequest => EK::Internal,
204 E::HttpStatus(_, _) => EK::TorDirectoryError,
205 E::Tunnel(e) => e.kind(),
206 }
207 }
208}
209
210impl HasKind for RequestFailedError {
211 fn kind(&self) -> ErrorKind {
212 self.error.kind()
213 }
214}
215
216impl HasKind for Error {
217 fn kind(&self) -> ErrorKind {
218 use Error as E;
219 match self {
220 E::CircMgr(e) => e.kind(),
221 E::RequestFailed(e) => e.kind(),
222 E::Bug(e) => e.kind(),
223 }
224 }
225}
226
227#[cfg(any(feature = "hs-client", feature = "hs-service"))]
228impl Error {
229 pub fn should_report_as_suspicious_if_anon(&self) -> bool {
233 use Error as E;
234 match self {
235 E::CircMgr(_) => false,
236 E::RequestFailed(e) => e.error.should_report_as_suspicious_if_anon(),
237 E::Bug(_) => false,
238 }
239 }
240}
241#[cfg(any(feature = "hs-client", feature = "hs-service"))]
242impl RequestError {
243 pub fn should_report_as_suspicious_if_anon(&self) -> bool {
247 use tor_proto::Error as PE;
248 match self {
249 RequestError::ResponseTooLong(_) => true,
250 RequestError::HeadersTooLong(_) => true,
251 RequestError::Proto(PE::ExcessInboundCells) => true,
252 RequestError::Proto(_) => false,
253 RequestError::DirTimeout => false,
254 RequestError::TruncatedHeaders => false,
255 RequestError::Utf8Encoding(_) => false,
256 RequestError::IoError(_) => false,
257 RequestError::HttparseError(_) => false,
258 RequestError::HttpError(_) => false,
259 RequestError::ContentEncoding(_) => false,
260 RequestError::TooMuchClockSkew => false,
261 RequestError::EmptyRequest => false,
262 RequestError::HttpStatus(_, _) => false,
263 RequestError::Tunnel(_) => false,
264 }
265 }
266}