1//! Declare dirclient-specific errors.
23use std::sync::Arc;
45use thiserror::Error;
6use tor_error::{Bug, ErrorKind, HasKind};
7use tor_linkspec::OwnedChanTarget;
8use tor_rtcompat::TimeoutError;
910use crate::SourceInfo;
1112/// An error originating from the tor-dirclient crate.
13#[derive(Error, Debug, Clone)]
14#[non_exhaustive]
15#[allow(clippy::large_enum_variant)] // TODO(nickm) worth fixing as we do #587
16pub enum Error {
17/// Error while getting a circuit
18#[error("Error while getting a circuit")]
19CircMgr(#[from] tor_circmgr::Error),
2021/// An error that has occurred after we have contacted a directory cache and made a circuit to it.
22#[error("Error fetching directory information")]
23RequestFailed(#[from] RequestFailedError),
2425/// We ran into a problem that is probably due to a programming issue.
26#[error("Internal error")]
27Bug(#[from] Bug),
28}
2930/// An error that has occurred after we have contacted a directory cache and made a circuit to it.
31#[derive(Error, Debug, Clone)]
32#[allow(clippy::exhaustive_structs)] // TODO should not be exhaustive
33#[error("Request failed{}", FromSource(.source))]
34pub struct RequestFailedError {
35/// The source that gave us this error.
36pub source: Option<SourceInfo>,
3738/// The underlying error that occurred.
39#[source]
40pub error: RequestError,
41}
4243/// Helper type to display an optional source of directory information.
44struct FromSource<'a>(&'a Option<SourceInfo>);
4546impl std::fmt::Display for FromSource<'_> {
47fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48if let Some(si) = self.0 {
49write!(f, " from {}", si)
50 } else {
51Ok(())
52 }
53 }
54}
5556/// An error originating from the tor-dirclient crate.
57#[derive(Error, Debug, Clone)]
58#[non_exhaustive]
59pub enum RequestError {
60/// The directory cache took too long to reply to us.
61#[error("directory timed out")]
62DirTimeout,
6364/// We got an EOF before we were done with the headers.
65#[error("truncated HTTP headers")]
66TruncatedHeaders,
6768/// Received a response that was longer than we expected.
69#[error("response too long; gave up after {0} bytes")]
70ResponseTooLong(usize),
7172/// Received too many bytes in our headers.
73#[error("headers too long; gave up after {0} bytes")]
74HeadersTooLong(usize),
7576/// Data received was not UTF-8 encoded.
77#[error("Couldn't decode data as UTF-8.")]
78Utf8Encoding(#[from] std::string::FromUtf8Error),
7980/// Io error while reading on connection
81#[error("IO error")]
82IoError(#[source] Arc<std::io::Error>),
8384/// A protocol error while launching a stream
85#[error("Protocol error while launching a stream")]
86Proto(#[from] tor_proto::Error),
8788/// A protocol error while launching a stream
89#[error("Tunnel error")]
90Tunnel(#[from] tor_circmgr::Error),
9192/// Error when parsing http
93#[error("Couldn't parse HTTP headers")]
94HttparseError(#[from] httparse::Error),
9596/// Error while creating http request
97//
98 // TODO this should be abolished, in favour of a `Bug` variant,
99 // so that we get a stack trace, as per the notes for EK::Internal.
100 // We could convert via into_internal!, or a custom `From` impl.
101#[error("Couldn't create HTTP request")]
102HttpError(#[source] Arc<http::Error>),
103104/// Unrecognized content-encoding
105#[error("Unrecognized content encoding: {0:?}")]
106ContentEncoding(String),
107108/// Too much clock skew between us and the directory.
109 ///
110 /// (We've giving up on this request early, since any directory that it
111 /// believes in, we would reject as untimely.)
112#[error("Too much clock skew with directory cache")]
113TooMuchClockSkew,
114115/// We tried to launch a request without any requested objects.
116 ///
117 /// This can happen if (for example) we request an empty list of
118 /// microdescriptors or certificates.
119#[error("We didn't have any objects to request")]
120EmptyRequest,
121122/// HTTP status code indicates a not completely successful request
123#[error("HTTP status code {0}: {1:?}")]
124HttpStatus(u16, String),
125}
126127impl From<TimeoutError> for RequestError {
128fn from(_: TimeoutError) -> Self {
129 RequestError::DirTimeout
130 }
131}
132133impl From<std::io::Error> for RequestError {
134fn from(err: std::io::Error) -> Self {
135Self::IoError(Arc::new(err))
136 }
137}
138139impl From<http::Error> for RequestError {
140fn from(err: http::Error) -> Self {
141Self::HttpError(Arc::new(err))
142 }
143}
144145impl Error {
146/// Return true if this error means that the circuit shouldn't be used
147 /// for any more directory requests.
148pub fn should_retire_circ(&self) -> bool {
149// TODO: probably this is too aggressive, and we should
150 // actually _not_ dump the circuit under all circumstances.
151match self {
152 Error::CircMgr(_) => true, // should be unreachable.
153Error::RequestFailed(RequestFailedError { error, .. }) => error.should_retire_circ(),
154 Error::Bug(_) => true,
155 }
156 }
157158/// Return the peer or peers that are to be blamed for the error.
159 ///
160 /// (This can return multiple peers if the request failed because multiple
161 /// circuit attempts all failed.)
162pub fn cache_ids(&self) -> Vec<&OwnedChanTarget> {
163match &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}
173174impl RequestError {
175/// Return true if this error means that the circuit shouldn't be used
176 /// for any more directory requests.
177pub fn should_retire_circ(&self) -> bool {
178// TODO: probably this is too aggressive, and we should
179 // actually _not_ dump the circuit under all circumstances.
180true
181}
182}
183184impl HasKind for RequestError {
185fn kind(&self) -> ErrorKind {
186use ErrorKind as EK;
187use RequestError as E;
188match 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// TODO: it would be good to get more information out of the IoError
195 // in this case, but that would require a bunch of gnarly
196 // downcasting.
197E::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}
209210impl HasKind for RequestFailedError {
211fn kind(&self) -> ErrorKind {
212self.error.kind()
213 }
214}
215216impl HasKind for Error {
217fn kind(&self) -> ErrorKind {
218use Error as E;
219match self {
220 E::CircMgr(e) => e.kind(),
221 E::RequestFailed(e) => e.kind(),
222 E::Bug(e) => e.kind(),
223 }
224 }
225}
226227#[cfg(any(feature = "hs-client", feature = "hs-service"))]
228impl Error {
229/// Return true if this error is one that we should report as a suspicious event,
230 /// along with the dirserver and description of the relevant document,
231 /// if the request was made anonymously.
232pub fn should_report_as_suspicious_if_anon(&self) -> bool {
233use Error as E;
234match 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/// Return true if this error is one that we should report as a suspicious event,
244 /// along with the dirserver and description of the relevant document,
245 /// if the request was made anonymously.
246pub fn should_report_as_suspicious_if_anon(&self) -> bool {
247use tor_proto::Error as PE;
248match 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}