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("HTTP status code {0}: {1:?}")]
130 HttpStatus(u16, String),
131}
132
133impl From<TimeoutError> for RequestError {
134 fn from(_: TimeoutError) -> Self {
135 RequestError::DirTimeout
136 }
137}
138
139impl From<std::io::Error> for RequestError {
140 fn from(err: std::io::Error) -> Self {
141 Self::IoError(Arc::new(err))
142 }
143}
144
145impl From<http::Error> for RequestError {
146 fn from(err: http::Error) -> Self {
147 Self::HttpError(Arc::new(err))
148 }
149}
150
151impl Error {
152 pub fn should_retire_circ(&self) -> bool {
155 match self {
158 Error::CircMgr(_) => true, Error::RequestFailed(RequestFailedError { error, .. }) => error.should_retire_circ(),
160 Error::Bug(_) => true,
161 }
162 }
163
164 pub fn cache_ids(&self) -> Vec<&OwnedChanTarget> {
169 match &self {
170 Error::CircMgr(e) => e.peers(),
171 Error::RequestFailed(RequestFailedError {
172 source: Some(source),
173 ..
174 }) => vec![source.cache_id()],
175 _ => Vec::new(),
176 }
177 }
178}
179
180impl RequestError {
181 pub fn should_retire_circ(&self) -> bool {
184 true
187 }
188}
189
190impl HasKind for RequestError {
191 fn kind(&self) -> ErrorKind {
192 use ErrorKind as EK;
193 use RequestError as E;
194 match self {
195 E::DirTimeout => EK::TorNetworkTimeout,
196 E::TruncatedHeaders => EK::TorProtocolViolation,
197 E::ResponseTooLong(_) => EK::TorProtocolViolation,
198 E::HeadersTooLong(_) => EK::TorProtocolViolation,
199 E::Utf8Encoding(_) => EK::TorProtocolViolation,
200 E::IoError(_) => EK::TorDirectoryError,
204 E::Proto(e) => e.kind(),
205 E::HttparseError(_) => EK::TorProtocolViolation,
206 E::HttpError(_) => EK::Internal,
207 E::ContentEncoding(_) => EK::TorProtocolViolation,
208 E::TooMuchClockSkew => EK::TorDirectoryError,
209 E::EmptyRequest => EK::Internal,
210 E::HttpStatus(_, _) => EK::TorDirectoryError,
211 E::Tunnel(e) => e.kind(),
212 }
213 }
214}
215
216impl HasKind for RequestFailedError {
217 fn kind(&self) -> ErrorKind {
218 self.error.kind()
219 }
220}
221
222impl HasKind for Error {
223 fn kind(&self) -> ErrorKind {
224 use Error as E;
225 match self {
226 E::CircMgr(e) => e.kind(),
227 E::RequestFailed(e) => e.kind(),
228 E::Bug(e) => e.kind(),
229 }
230 }
231}
232
233#[cfg(any(feature = "hs-client", feature = "hs-service"))]
234impl Error {
235 pub fn should_report_as_suspicious_if_anon(&self) -> bool {
239 use Error as E;
240 match self {
241 E::CircMgr(_) => false,
242 E::RequestFailed(e) => e.error.should_report_as_suspicious_if_anon(),
243 E::Bug(_) => false,
244 }
245 }
246}
247#[cfg(any(feature = "hs-client", feature = "hs-service"))]
248impl RequestError {
249 pub fn should_report_as_suspicious_if_anon(&self) -> bool {
253 use tor_proto::Error as PE;
254 match self {
255 RequestError::ResponseTooLong(_) => true,
256 RequestError::HeadersTooLong(_) => true,
257 RequestError::Proto(PE::ExcessInboundCells) => true,
258 RequestError::Proto(_) => false,
259 RequestError::DirTimeout => false,
260 RequestError::TruncatedHeaders => false,
261 RequestError::Utf8Encoding(_) => false,
262 RequestError::IoError(_) => false,
263 RequestError::HttparseError(_) => false,
264 RequestError::HttpError(_) => false,
265 RequestError::ContentEncoding(_) => false,
266 RequestError::TooMuchClockSkew => false,
267 RequestError::EmptyRequest => false,
268 RequestError::HttpStatus(_, _) => false,
269 RequestError::Tunnel(_) => false,
270 }
271 }
272}