1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Error types for the vanguards subsystem.

use std::sync::Arc;

use futures::task::SpawnError;
use tor_error::{ErrorKind, HasKind};

use crate::vanguards::{Layer, VanguardMode};

/// An error coming from the vanguards subsystem.
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum VanguardMgrError {
    /// Attempted to use an unbootstrapped `VanguardMgr` for something that
    /// requires bootstrapping to have completed.
    #[error("Cannot {action} with unbootstrapped vanguard manager")]
    BootstrapRequired {
        /// What we were trying to do that required bootstrapping.
        action: &'static str,
    },

    /// Attempted to select a vanguard layer that is not supported in the current [`VanguardMode`],
    #[error("{layer} vanguards are not supported in {mode} mode")]
    LayerNotSupported {
        /// The layer we tried to select a vanguard for.
        layer: Layer,
        /// The [`VanguardMode`] we are in.
        mode: VanguardMode,
    },

    /// Could not find a suitable relay to use for the specifier layer.
    #[error("No suitable relays")]
    NoSuitableRelay(Layer),

    /// Could not get timely network directory.
    #[error("Unable to get timely network directory")]
    NetDir(#[from] tor_netdir::Error),

    /// Failed to access persistent storage.
    #[error("Failed to access persistent vanguard state")]
    State(#[from] tor_persist::Error),

    /// Could not spawn a task.
    #[error("Unable to spawn a task")]
    Spawn(#[source] Arc<SpawnError>),

    /// An internal error occurred.
    #[error("Internal error")]
    Bug(#[from] tor_error::Bug),
}

impl HasKind for VanguardMgrError {
    fn kind(&self) -> ErrorKind {
        match self {
            VanguardMgrError::BootstrapRequired { .. } => ErrorKind::BootstrapRequired,
            VanguardMgrError::LayerNotSupported { .. } => ErrorKind::BadApiUsage,
            VanguardMgrError::NoSuitableRelay(_) => ErrorKind::NoPath,
            VanguardMgrError::NetDir(e) => e.kind(),
            VanguardMgrError::State(e) => e.kind(),
            VanguardMgrError::Spawn(e) => e.kind(),
            VanguardMgrError::Bug(e) => e.kind(),
        }
    }
}