1
//! Declare dirclient-specific errors.
2

            
3
use std::sync::Arc;
4

            
5
use thiserror::Error;
6
use tor_error::{ErrorKind, HasKind};
7
use tor_rtcompat::TimeoutError;
8

            
9
/// An error originating from the tor-dirclient crate.
10
#[derive(Error, Debug, Clone)]
11
#[non_exhaustive]
12
pub enum Error {
13
    /// The directory cache took too long to reply to us.
14
    #[error("directory timed out")]
15
    DirTimeout,
16

            
17
    /// We got an EOF before we were done with the headers.
18
    #[error("truncated HTTP headers")]
19
    TruncatedHeaders,
20

            
21
    /// Received a response that was longer than we expected.
22
    #[error("response too long; gave up after {0} bytes")]
23
    ResponseTooLong(usize),
24

            
25
    /// Data received was not UTF-8 encoded.
26
    #[error("Couldn't decode data as UTF-8.")]
27
    Utf8Encoding(#[from] std::string::FromUtf8Error),
28

            
29
    /// Io error while reading on connection
30
    #[error("IO error: {0}")]
31
    IoError(#[source] Arc<std::io::Error>),
32

            
33
    /// A protocol error while launching a stream
34
    #[error("Protocol error while launching a stream: {0}")]
35
    Proto(#[from] tor_proto::Error),
36

            
37
    /// Error while getting a circuit
38
    #[error("Error while getting a circuit {0}")]
39
    CircMgr(#[from] tor_circmgr::Error),
40

            
41
    /// Error when parsing http
42
    #[error("Couldn't parse HTTP headers")]
43
    HttparseError(#[from] httparse::Error),
44

            
45
    /// Error while creating http request
46
    //
47
    // TODO this should be abolished, in favour of a `Bug` variant,
48
    // so that we get a stack trace, as per the notes for EK::Internal.
49
    // We could convert via into_internal!, or a custom `From` impl.
50
    #[error("Couldn't create HTTP request")]
51
    HttpError(#[source] Arc<http::Error>),
52

            
53
    /// Unrecognized content-encoding
54
    #[error("Unrecognized content encoding: {0:?}")]
55
    ContentEncoding(String),
56
}
57

            
58
impl From<TimeoutError> for Error {
59
    fn from(_: TimeoutError) -> Self {
60
        Error::DirTimeout
61
    }
62
}
63

            
64
impl From<std::io::Error> for Error {
65
3
    fn from(err: std::io::Error) -> Self {
66
3
        Self::IoError(Arc::new(err))
67
3
    }
68
}
69

            
70
impl From<http::Error> for Error {
71
    fn from(err: http::Error) -> Self {
72
        Self::HttpError(Arc::new(err))
73
    }
74
}
75

            
76
impl Error {
77
    /// Return true if this error means that the circuit shouldn't be used
78
    /// for any more directory requests.
79
1
    pub fn should_retire_circ(&self) -> bool {
80
1
        // TODO: probably this is too aggressive, and we should
81
1
        // actually _not_ dump the circuit under all circumstances.
82
1
        true
83
1
    }
84
}
85

            
86
impl HasKind for Error {
87
    fn kind(&self) -> ErrorKind {
88
        use Error as E;
89
        use ErrorKind as EK;
90
        match self {
91
            E::DirTimeout => EK::TorNetworkTimeout,
92
            E::TruncatedHeaders => EK::TorProtocolViolation,
93
            E::ResponseTooLong(_) => EK::TorProtocolViolation,
94
            E::Utf8Encoding(_) => EK::TorProtocolViolation,
95
            // TODO: it would be good to get more information out of the IoError
96
            // in this case, but that would require a bunch of gnarly
97
            // downcasting.
98
            E::IoError(_) => EK::TorDirectoryError,
99
            E::Proto(e) => e.kind(),
100
            E::CircMgr(e) => e.kind(),
101
            E::HttparseError(_) => EK::TorProtocolViolation,
102
            E::HttpError(_) => EK::Internal,
103
            E::ContentEncoding(_) => EK::TorProtocolViolation,
104
        }
105
    }
106
}