Skip to content
Snippets Groups Projects
Commit 5b04e5a6 authored by Nick Mathewson's avatar Nick Mathewson :game_die:
Browse files

guardmgr: move error types into new err.rs module.

This is more in keeping with the rest of our code.
parent 80b65c3a
No related branches found
No related tags found
1 merge request!433Refactor FallbackDir handling and implement a retry-after-delay mechanism.
//! Declare error types for the `tor-guardmgr` crate.
use futures::task::SpawnError;
use std::sync::Arc;
use std::time::Instant;
use tor_error::{ErrorKind, HasKind};
/// A error caused by a failure to pick a guard.
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum PickGuardError {
/// All members of the current sample were down.
#[error("All guards are down")]
AllGuardsDown {
/// The next time at which any guard will be retriable.
retry_at: Option<Instant>,
},
/// Some guards were running, but all of them were either blocked on pending
/// circuits at other guards, unusable for the provided purpose, or filtered
/// out.
#[error("No running guards were usable for the selected purpose")]
NoGuardsUsable,
}
impl tor_error::HasKind for PickGuardError {
fn kind(&self) -> tor_error::ErrorKind {
use tor_error::ErrorKind as EK;
use PickGuardError as E;
match self {
E::AllGuardsDown { .. } => EK::TorAccessFailed,
E::NoGuardsUsable => EK::NoPath,
}
}
}
/// An error caused while creating or updating a guard manager.
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum GuardMgrError {
/// An error manipulating persistent state
#[error("Problem accessing persistent state")]
State(#[from] tor_persist::Error),
/// An error that occurred while trying to spawn a daemon task.
#[error("Unable to spawn {spawning}")]
Spawn {
/// What we were trying to spawn.
spawning: &'static str,
/// What happened when we tried to spawn it.
#[source]
cause: Arc<SpawnError>,
},
}
impl HasKind for GuardMgrError {
#[rustfmt::skip] // to preserve table in match
fn kind(&self) -> ErrorKind {
use GuardMgrError as G;
match self {
G::State(e) => e.kind(),
G::Spawn{ cause, .. } => cause.kind(),
}
}
}
impl GuardMgrError {
/// Construct a new `GuardMgrError` from a `SpawnError`.
pub(crate) fn from_spawn(spawning: &'static str, err: SpawnError) -> GuardMgrError {
GuardMgrError::Spawn {
spawning,
cause: Arc::new(err),
}
}
}
......@@ -132,7 +132,7 @@
use educe::Educe;
use futures::channel::mpsc;
use futures::task::{SpawnError, SpawnExt};
use futures::task::SpawnExt;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::convert::{TryFrom, TryInto};
......@@ -141,13 +141,13 @@ use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant, SystemTime};
use tracing::{debug, info, trace, warn};
use tor_error::{ErrorKind, HasKind};
use tor_llcrypto::pk;
use tor_netdir::{params::NetParameters, NetDir, Relay};
use tor_persist::{DynStorageHandle, StateMgr};
use tor_rtcompat::Runtime;
mod daemon;
mod err;
pub mod fallback;
mod filter;
mod guard;
......@@ -155,9 +155,9 @@ mod pending;
mod sample;
mod util;
pub use err::{GuardMgrError, PickGuardError};
pub use filter::GuardFilter;
pub use pending::{GuardMonitor, GuardStatus, GuardUsable};
pub use sample::PickGuardError;
use pending::{PendingRequest, RequestId};
use sample::GuardSet;
......@@ -1047,46 +1047,6 @@ pub enum GuardRestriction {
AvoidAllIds(HashSet<pk::ed25519::Ed25519Identity>),
}
/// An error caused while creating or updating a guard manager.
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum GuardMgrError {
/// An error manipulating persistent state
#[error("Problem accessing persistent state")]
State(#[from] tor_persist::Error),
/// An error that occurred while trying to spawn a daemon task.
#[error("Unable to spawn {spawning}")]
Spawn {
/// What we were trying to spawn.
spawning: &'static str,
/// What happened when we tried to spawn it.
#[source]
cause: Arc<SpawnError>,
},
}
impl HasKind for GuardMgrError {
#[rustfmt::skip] // to preserve table in match
fn kind(&self) -> ErrorKind {
use GuardMgrError as G;
match self {
G::State(e) => e.kind(),
G::Spawn{ cause, .. } => cause.kind(),
}
}
}
impl GuardMgrError {
/// Construct a new `GuardMgrError` from a `SpawnError`.
fn from_spawn(spawning: &'static str, err: SpawnError) -> GuardMgrError {
GuardMgrError::Spawn {
spawning,
cause: Arc::new(err),
}
}
}
#[cfg(test)]
mod test {
#![allow(clippy::unwrap_used)]
......
......@@ -3,7 +3,7 @@
use crate::filter::GuardFilter;
use crate::guard::{Guard, NewlyConfirmed, Reachable};
use crate::{ExternalFailure, GuardId, GuardParams, GuardUsage, GuardUsageKind};
use crate::{ExternalFailure, GuardId, GuardParams, GuardUsage, GuardUsageKind, PickGuardError};
use tor_netdir::{NetDir, Relay};
use itertools::Itertools;
......@@ -781,35 +781,6 @@ impl<'a> From<GuardSample<'a>> for GuardSet {
}
}
/// A error caused by a failure to pick a guard.
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum PickGuardError {
/// All members of the current sample were down.
#[error("All guards are down")]
AllGuardsDown {
/// The next time at which any guard will be retriable.
retry_at: Option<Instant>,
},
/// Some guards were running, but all of them were either blocked on pending
/// circuits at other guards, unusable for the provided purpose, or filtered
/// out.
#[error("No running guards were usable for the selected purpose")]
NoGuardsUsable,
}
impl tor_error::HasKind for PickGuardError {
fn kind(&self) -> tor_error::ErrorKind {
use tor_error::ErrorKind as EK;
use PickGuardError as E;
match self {
E::AllGuardsDown { .. } => EK::TorAccessFailed,
E::NoGuardsUsable => EK::NoPath,
}
}
}
#[cfg(test)]
mod test {
#![allow(clippy::unwrap_used)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment