1//! Error types for the vanguards subsystem.
23use std::sync::Arc;
45use futures::task::SpawnError;
6use tor_error::{ErrorKind, HasKind};
78use crate::vanguards::{Layer, VanguardMode};
910/// An error coming from the vanguards subsystem.
11#[derive(Clone, Debug, thiserror::Error)]
12#[non_exhaustive]
13pub enum VanguardMgrError {
14/// Attempted to use an unbootstrapped `VanguardMgr` for something that
15 /// requires bootstrapping to have completed.
16#[error("Cannot {action} with unbootstrapped vanguard manager")]
17BootstrapRequired {
18/// What we were trying to do that required bootstrapping.
19action: &'static str,
20 },
2122/// Attempted to select a vanguard layer that is not supported in the current [`VanguardMode`],
23#[error("{layer} vanguards are not supported in {mode} mode")]
24LayerNotSupported {
25/// The layer we tried to select a vanguard for.
26layer: Layer,
27/// The [`VanguardMode`] we are in.
28mode: VanguardMode,
29 },
3031/// Could not find a suitable relay to use for the specifier layer.
32#[error("No suitable relays")]
33NoSuitableRelay(Layer),
3435/// Could not get timely network directory.
36#[error("Unable to get timely network directory")]
37NetDir(#[from] tor_netdir::Error),
3839/// Failed to access persistent storage.
40#[error("Failed to access persistent vanguard state")]
41State(#[from] tor_persist::Error),
4243/// Could not spawn a task.
44#[error("Unable to spawn a task")]
45Spawn(#[source] Arc<SpawnError>),
4647/// An internal error occurred.
48#[error("Internal error")]
49Bug(#[from] tor_error::Bug),
50}
5152impl HasKind for VanguardMgrError {
53fn kind(&self) -> ErrorKind {
54match self {
55 VanguardMgrError::BootstrapRequired { .. } => ErrorKind::BootstrapRequired,
56 VanguardMgrError::LayerNotSupported { .. } => ErrorKind::BadApiUsage,
57 VanguardMgrError::NoSuitableRelay(_) => ErrorKind::NoPath,
58 VanguardMgrError::NetDir(e) => e.kind(),
59 VanguardMgrError::State(e) => e.kind(),
60 VanguardMgrError::Spawn(e) => e.kind(),
61 VanguardMgrError::Bug(e) => e.kind(),
62 }
63 }
64}