Skip to main content

tor_dirserver/
err.rs

1//! Error module for `tor-dirserver`.
2
3use thiserror::Error;
4use tor_netdoc::parse2::VerifyFailed;
5
6/// Indicates that an error variant is fatal.
7///
8/// A fatal error means that the application should abort execution and may
9/// not retry again in the future.
10///
11/// This trait should only be implemented for error variants where some error
12/// variants are fatal and others are not.  In other words: An error where all
13/// variants are either fatal or non-fatal does not qualify for this trait.
14// TODO DIRMIRROR: Move this to tor_error.
15pub(crate) trait IsFatal: std::error::Error {
16    /// Checks whether the current error is considered to be fatal.
17    fn is_fatal(&self) -> bool;
18}
19
20/// An error while performing a request at a directory authority.
21#[derive(Debug, Error)]
22#[non_exhaustive]
23pub(crate) enum AuthorityRequestError {
24    /// TCP connection to the endpoint failed.
25    #[error("tcp connection error: {0}")]
26    TcpConnect(std::io::Error),
27
28    /// [`tor_dirclient`] failed at performing the request.
29    ///
30    /// This usually indicates some failures in HTTP/1.0, such as the response
31    /// not being valid HTTP/1.0; although Tor generally has some specialities
32    /// here and there with regard to this, hence why we have dirclient in the
33    /// first place.
34    #[error("dirclient error: {0}")]
35    Request(Box<tor_dirclient::RequestFailedError>),
36
37    /// A response does not make semantic sense.
38    ///
39    /// This for example may include cases where we got more netdocs than we
40    /// requested for.
41    #[error("response error: {0}")]
42    Response(&'static str),
43
44    /// An internal error.
45    #[error("internal error")]
46    Bug(#[from] tor_error::Bug),
47}
48
49impl From<tor_dirclient::RequestFailedError> for AuthorityRequestError {
50    fn from(value: tor_dirclient::RequestFailedError) -> Self {
51        Self::Request(Box::new(value))
52    }
53}
54
55impl IsFatal for AuthorityRequestError {
56    /// The [`AuthorityRequestError`] is considered to be fatal.
57    ///
58    /// Right now, the following variants are considered to be fatal:
59    /// * [`AuthorityRequestError::Bug`]
60    fn is_fatal(&self) -> bool {
61        matches!(&self, Self::Bug(_))
62    }
63}
64
65/// An error while interacting with a database.
66///
67/// This error should be returned by all functions that interact with the
68/// database in one way or another.
69#[derive(Debug, Error)]
70#[non_exhaustive]
71pub(crate) enum DatabaseError {
72    /// A low-level SQLite error has occurred, which can have a basically
73    /// infinite amount of reasons, all of them outlined in the actual SQLite
74    /// and [`rusqlite`] documentation.
75    #[error("low-level rusqlite error: {0}")]
76    LowLevel(#[from] rusqlite::Error),
77
78    /// This is an application level error meaning that the database can be
79    /// successfully accessed but its content implies it is of a schema version
80    /// we do not support.
81    ///
82    /// Keep in mind that an unrecognized schema is not equal to no schema.
83    /// In the latter case we actually initialize the database, whereas in the
84    /// previous one, we fail early in order to not corrupt an existing database.
85    /// Future versions of this crate should continue with this promise in order
86    /// to ensure forward compatibility.
87    #[error("incompatible schema version: {version}")]
88    IncompatibleSchema {
89        /// The incompatible schema version found in the database.
90        version: String,
91    },
92
93    /// Interaction with our database pool, [`r2d2`], has failed.
94    ///
95    /// Unlike other database pools, this error is fairly straightforward and
96    /// may only be obtained in the cases in which we try to obtain a connection
97    /// handle from the pool.  Notably, it does not fail if, for example,
98    /// the low-level [`rusqlite`] has a failure.
99    #[error("pool error: {0}")]
100    Pool(#[from] r2d2::Error),
101
102    /// An internal error.
103    #[error("Internal error")]
104    Bug(#[from] tor_error::Bug),
105}
106
107/// An error related to an operation in the dirmirror FSM.
108//
109// TODO: Rename this to MirrorOperationError.
110#[derive(Debug, Error)]
111#[non_exhaustive]
112pub(crate) enum OperationError {
113    /// Request to a directory authority failed.
114    #[error("authority request error: {0}")]
115    AuthorityRequest(#[from] Box<AuthorityRequestError>),
116
117    /// Access to the database failed for good.
118    #[error("database error: {0}")]
119    Database(#[from] DatabaseError),
120
121    /// Verificaton of a network document has failed.
122    ///
123    /// This also includes checks for timeliness.
124    ///
125    /// We do not consider this fatal but simply discard the unverified network
126    /// document.
127    #[error("verification error: {0}")]
128    VerifyFailed(#[from] VerifyFailed),
129
130    /// An internal error.
131    #[error("Internal error")]
132    Bug(#[from] tor_error::Bug),
133}
134
135impl From<AuthorityRequestError> for OperationError {
136    fn from(value: AuthorityRequestError) -> Self {
137        Self::AuthorityRequest(Box::new(value))
138    }
139}
140
141impl IsFatal for OperationError {
142    /// The [`OperationError`] is considered to be fatal.
143    ///
144    /// Right now, the following variants are considered to be fatal:
145    /// * [`OperationError::Database`]
146    /// * [`OperationError::Bug`]
147    fn is_fatal(&self) -> bool {
148        matches!(&self, Self::Database(_) | Self::Bug(_))
149    }
150}