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)]
36#[allow(clippy::exhaustive_structs)] #[error("Request failed from {}: {error}", FromSource(.source))]
38pub struct RequestFailedError {
39 pub source: Option<SourceInfo>,
41
42 #[source]
44 pub error: RequestError,
45}
46
47struct FromSource<'a>(&'a Option<SourceInfo>);
49
50impl std::fmt::Display for FromSource<'_> {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 if let Some(si) = self.0 {
53 write!(f, "{}", si)
54 } else {
55 write!(f, "N/A")
56 }
57 }
58}
59
60#[derive(Error, Debug, Clone)]
64#[non_exhaustive]
65pub enum RequestError {
66 #[error("directory timed out")]
68 DirTimeout,
69
70 #[error("truncated HTTP headers")]
72 TruncatedHeaders,
73
74 #[error("response too long; gave up after {0} bytes")]
76 ResponseTooLong(usize),
77
78 #[error("headers too long; gave up after {0} bytes")]
80 HeadersTooLong(usize),
81
82 #[error("Couldn't decode data as UTF-8.")]
84 Utf8Encoding(#[from] std::string::FromUtf8Error),
85
86 #[error("IO error")]
88 IoError(#[source] Arc<std::io::Error>),
89
90 #[error("Protocol error while launching a stream")]
92 Proto(#[from] tor_proto::Error),
93
94 #[error("Tunnel error")]
96 Tunnel(#[from] tor_circmgr::Error),
97
98 #[error("Couldn't parse HTTP headers")]
100 HttparseError(#[from] httparse::Error),
101
102 #[error("Couldn't create HTTP request")]
108 HttpError(#[source] Arc<http::Error>),
109
110 #[error("Unrecognized content encoding: {0:?}")]
112 ContentEncoding(String),
113
114 #[error("Too much clock skew with directory cache")]
119 TooMuchClockSkew,
120
121 #[error("We didn't have any objects to request")]
126 EmptyRequest,
127
128 #[error("Empty successful response")]
133 EmptyResponse,
134
135 #[error("HTTP status code {0}: {1:?}")]
137 HttpStatus(u16, String),
138}
139
140impl From<TimeoutError> for RequestError {
141 fn from(_: TimeoutError) -> Self {
142 RequestError::DirTimeout
143 }
144}
145
146impl From<std::io::Error> for RequestError {
147 fn from(err: std::io::Error) -> Self {
148 Self::IoError(Arc::new(err))
149 }
150}
151
152impl From<http::Error> for RequestError {
153 fn from(err: http::Error) -> Self {
154 Self::HttpError(Arc::new(err))
155 }
156}
157
158impl Error {
159 pub fn should_retire_circ(&self) -> bool {
162 match self {
165 Error::CircMgr(_) => true, Error::RequestFailed(RequestFailedError { error, .. }) => error.should_retire_circ(),
167 Error::Bug(_) => true,
168 }
169 }
170
171 pub fn cache_ids(&self) -> Vec<&OwnedChanTarget> {
176 match &self {
177 Error::CircMgr(e) => e.peers(),
178 Error::RequestFailed(RequestFailedError {
179 source: Some(source),
180 ..
181 }) => vec![source.cache_id()],
182 _ => Vec::new(),
183 }
184 }
185}
186
187impl RequestError {
188 pub fn should_retire_circ(&self) -> bool {
191 true
194 }
195}
196
197impl HasKind for RequestError {
198 fn kind(&self) -> ErrorKind {
199 use ErrorKind as EK;
200 use RequestError as E;
201 match self {
202 E::DirTimeout => EK::TorNetworkTimeout,
203 E::TruncatedHeaders => EK::TorProtocolViolation,
204 E::ResponseTooLong(_) => EK::TorProtocolViolation,
205 E::HeadersTooLong(_) => EK::TorProtocolViolation,
206 E::Utf8Encoding(_) => EK::TorProtocolViolation,
207 E::IoError(_) => EK::TorDirectoryError,
211 E::Proto(e) => e.kind(),
212 E::HttparseError(_) => EK::TorProtocolViolation,
213 E::HttpError(_) => EK::Internal,
214 E::ContentEncoding(_) => EK::TorProtocolViolation,
215 E::TooMuchClockSkew => EK::TorDirectoryError,
216 E::EmptyRequest => EK::Internal,
217 E::EmptyResponse => EK::TorProtocolViolation,
218 E::HttpStatus(_, _) => EK::TorDirectoryError,
219 E::Tunnel(e) => e.kind(),
220 }
221 }
222}
223
224impl HasKind for RequestFailedError {
225 fn kind(&self) -> ErrorKind {
226 self.error.kind()
227 }
228}
229
230impl HasKind for Error {
231 fn kind(&self) -> ErrorKind {
232 use Error as E;
233 match self {
234 E::CircMgr(e) => e.kind(),
235 E::RequestFailed(e) => e.kind(),
236 E::Bug(e) => e.kind(),
237 }
238 }
239}
240
241#[cfg(any(feature = "hs-client", feature = "hs-service"))]
242impl Error {
243 pub fn should_report_as_suspicious_if_anon(&self) -> bool {
247 use Error as E;
248 match self {
249 E::CircMgr(_) => false,
250 E::RequestFailed(e) => e.error.should_report_as_suspicious_if_anon(),
251 E::Bug(_) => false,
252 }
253 }
254}
255#[cfg(any(feature = "hs-client", feature = "hs-service"))]
256impl RequestError {
257 pub fn should_report_as_suspicious_if_anon(&self) -> bool {
261 use tor_proto::Error as PE;
262 match self {
263 RequestError::ResponseTooLong(_) => true,
264 RequestError::HeadersTooLong(_) => true,
265 RequestError::Proto(PE::ExcessInboundCells) => true,
266 RequestError::Proto(_) => false,
267 RequestError::DirTimeout => false,
268 RequestError::TruncatedHeaders => false,
269 RequestError::Utf8Encoding(_) => false,
270 RequestError::IoError(_) => false,
271 RequestError::HttparseError(_) => false,
272 RequestError::HttpError(_) => false,
273 RequestError::ContentEncoding(_) => false,
274 RequestError::TooMuchClockSkew => false,
275 RequestError::EmptyRequest => false,
276 RequestError::EmptyResponse => false,
277 RequestError::HttpStatus(_, _) => false,
278 RequestError::Tunnel(_) => false,
279 }
280 }
281}