1
//! Declare error type for tor-netdir
2

            
3
use thiserror::Error;
4
use tor_error::HasKind;
5

            
6
/// An error returned by the network directory code
7
#[derive(Error, Clone, Debug)]
8
#[non_exhaustive]
9
pub enum Error {
10
    /// We don't have enough directory info to build circuits
11
    #[error("Not enough directory information to build circuits")]
12
    NotEnoughInfo,
13
    /// We don't have any directory information.
14
    #[error("No directory information available")]
15
    NoInfo,
16
    /// We have directory information, but it is too expired to use.
17
    #[error("Directory is expired, and we haven't got a new one yet")]
18
    DirExpired,
19
    /// We have directory information, but it is too expired to use.
20
    #[error("Directory is published too far in the future: Your clock is probably wrong")]
21
    DirNotYetValid,
22
    /// We received a consensus document that should be impossible.
23
    #[error("Invalid information from consensus document: {0}")]
24
    InvalidConsensus(&'static str),
25
}
26

            
27
impl HasKind for Error {
28
    fn kind(&self) -> tor_error::ErrorKind {
29
        use Error as E;
30
        use tor_error::ErrorKind as EK;
31
        match self {
32
            E::DirExpired => EK::DirectoryExpired,
33
            E::DirNotYetValid => EK::ClockSkew,
34
            E::NotEnoughInfo | E::NoInfo => EK::BootstrapRequired,
35
            E::InvalidConsensus(_) => EK::TorProtocolViolation,
36
        }
37
    }
38
}
39

            
40
/// An error that has occurred while trying to decode a set of externally provided link specifiers
41
/// into a reasonable [`VerbatimLinkSpecCircTarget`](tor_linkspec::verbatim::VerbatimLinkSpecCircTarget).
42
#[derive(Error, Clone, Debug)]
43
#[non_exhaustive]
44
#[cfg(feature = "hs-common")]
45
pub enum VerbatimCircTargetDecodeError {
46
    /// We failed to interpret the provided link specs, or didn't find enough detail in them.
47
    #[error("Unable to decode relay information")]
48
    CantDecode(#[from] tor_linkspec::decode::ChanTargetDecodeError),
49

            
50
    /// When we went to look up the relay, we found that the identities were not compatible with one another.
51
    #[error("Impossible combination of identities")]
52
    ImpossibleIds(#[source] crate::RelayLookupError),
53

            
54
    /// The onion key type was one that we don't support.
55
    #[error("Received an unsupported onion key type")]
56
    UnsupportedOnionKey,
57

            
58
    /// An internal error occurred, probably due to a programming mistake.
59
    #[error("Internal error")]
60
    Internal(#[from] tor_error::Bug),
61
}
62

            
63
/// An error returned when looking up onion service directories.
64
#[derive(Error, Clone, Debug)]
65
#[cfg(feature = "hs-common")]
66
#[non_exhaustive]
67
pub enum OnionDirLookupError {
68
    /// We tried to look up an onion service directory for a time period that
69
    /// did not correspond to one of our hash rings.
70
    #[error("Tried to look up an onion service directory for an invalid time period.")]
71
    WrongTimePeriod,
72
}
73

            
74
#[cfg(feature = "hs-common")]
75
impl HasKind for OnionDirLookupError {
76
    fn kind(&self) -> tor_error::ErrorKind {
77
        use OnionDirLookupError as E;
78
        use tor_error::ErrorKind as EK;
79
        match self {
80
            E::WrongTimePeriod => EK::BadApiUsage,
81
        }
82
    }
83
}