tor-chanmgr: use Bug types.

This commit is contained in:
Nick Mathewson 2022-02-15 14:19:03 -05:00
parent 10bca35bba
commit caa70fde35
4 changed files with 24 additions and 16 deletions

View File

@ -6,6 +6,7 @@ use std::sync::Mutex;
use crate::{event::ChanMgrEventSender, Error};
use std::time::Duration;
use tor_error::{bad_api_usage, internal};
use tor_linkspec::{ChanTarget, OwnedChanTarget};
use tor_llcrypto::pk;
use tor_rtcompat::{tls::TlsConnector, Runtime, TlsProvider};
@ -69,10 +70,9 @@ impl<R: Runtime> ChanBuilder<R> {
// TODO: This just uses the first address. Instead we could be
// smarter, or use "happy eyeballs", or whatever. Maybe we will
// want to refactor as we do so?
let addr = target
.addrs()
.get(0)
.ok_or_else(|| Error::UnusableTarget("No addresses for chosen relay".into()))?;
let addr = target.addrs().get(0).ok_or_else(|| {
Error::UnusableTarget(bad_api_usage!("No addresses for chosen relay"))
})?;
tracing::info!("Negotiating TLS with {}", addr);
@ -115,7 +115,7 @@ impl<R: Runtime> ChanBuilder<R> {
let peer_cert = tls
.peer_certificate()
.map_err(map_ioe("TLS certs"))?
.ok_or(Error::Internal("TLS connection with no peer certificate"))?;
.ok_or_else(|| Error::Internal(internal!("TLS connection with no peer certificate")))?;
{
self.event_sender

View File

@ -6,7 +6,7 @@ use std::sync::Arc;
use futures::task::SpawnError;
use thiserror::Error;
use tor_error::ErrorKind;
use tor_error::{internal, ErrorKind};
/// An error returned by a channel manager.
#[derive(Debug, Error, Clone)]
@ -14,7 +14,7 @@ use tor_error::ErrorKind;
pub enum Error {
/// A ChanTarget was given for which no channel could be built.
#[error("Target was unusable: {0}")]
UnusableTarget(String),
UnusableTarget(#[source] tor_error::Bug),
/// We were waiting on a pending channel, but it didn't succeed.
#[error("Pending channel failed to launch")]
@ -53,7 +53,7 @@ pub enum Error {
},
/// An internal error of some kind that should never occur.
#[error("Internal error: {0}")]
Internal(&'static str),
Internal(#[from] tor_error::Bug),
}
impl From<tor_rtcompat::TimeoutError> for Error {
@ -64,7 +64,7 @@ impl From<tor_rtcompat::TimeoutError> for Error {
impl<T> From<std::sync::PoisonError<T>> for Error {
fn from(_: std::sync::PoisonError<T>) -> Error {
Error::Internal("Thread failed while holding lock")
Error::Internal(internal!("Thread failed while holding lock"))
}
}

View File

@ -9,6 +9,7 @@ use futures::future::{FutureExt, Shared};
use rand::Rng;
use std::hash::Hash;
use std::time::Duration;
use tor_error::internal;
mod map;
@ -128,7 +129,7 @@ impl<CF: ChannelFactory> AbstractChanMgr<CF> {
const N_ATTEMPTS: usize = 2;
// TODO(nickm): It would be neat to use tor_retry instead.
let mut last_err = Err(Error::Internal("Error was never set!?"));
let mut last_err = Err(Error::Internal(internal!("Error was never set!?")));
for _ in 0..N_ATTEMPTS {
// First, see what state we're in, and what we should do
@ -158,7 +159,9 @@ impl<CF: ChannelFactory> AbstractChanMgr<CF> {
// is a bug.
(
None,
Action::Return(Err(Error::Internal("Found a poisoned entry"))),
Action::Return(Err(Error::Internal(internal!(
"Found a poisoned entry"
)))),
)
}
None => {
@ -183,7 +186,8 @@ impl<CF: ChannelFactory> AbstractChanMgr<CF> {
last_err = Err(e);
}
Err(_) => {
last_err = Err(Error::Internal("channel build task disappeared"));
last_err =
Err(Error::Internal(internal!("channel build task disappeared")));
}
},
// We need to launch a channel.
@ -257,6 +261,7 @@ mod test {
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tor_error::bad_api_usage;
use tor_rtcompat::{task::yield_now, test_with_one_runtime, Runtime};
@ -313,7 +318,7 @@ mod test {
let (ident, mood) = *target;
match mood {
// "X" means never connect.
'❌' | '🔥' => return Err(Error::UnusableTarget("emoji".into())),
'❌' | '🔥' => return Err(Error::UnusableTarget(bad_api_usage!("emoji"))),
// "zzz" means wait for 15 seconds then succeed.
'💤' => {
self.runtime.sleep(Duration::new(15, 0)).await;

View File

@ -6,6 +6,7 @@ use super::{AbstractChannel, Pending};
use crate::{Error, Result};
use std::collections::{hash_map, HashMap};
use tor_error::internal;
/// A map from channel id to channel state.
///
@ -62,7 +63,7 @@ impl<C: Clone> ChannelState<C> {
match self {
Open(ent) => Ok(Open(ent.clone())),
Building(pending) => Ok(Building(pending.clone())),
Poisoned(_) => Err(Error::Internal("Poisoned state in channel map")),
Poisoned(_) => Err(Error::Internal(internal!("Poisoned state in channel map"))),
}
}
@ -86,10 +87,12 @@ impl<C: AbstractChannel> ChannelState<C> {
if ent.channel.ident() == ident {
Ok(())
} else {
Err(Error::Internal("Identity mismatch"))
Err(Error::Internal(internal!("Identity mismatch")))
}
}
ChannelState::Poisoned(_) => Err(Error::Internal("Poisoned state in channel map")),
ChannelState::Poisoned(_) => {
Err(Error::Internal(internal!("Poisoned state in channel map")))
}
ChannelState::Building(_) => Ok(()),
}
}