tor-chanmgr: Handle IO errora in the new style

Two ? in the tests become expects, which will do.  That avoids having
to construct a proper error with context here.
This commit is contained in:
Ian Jackson 2022-01-26 15:44:12 +00:00
parent 2101dd5e39
commit 2a471ffd1a
2 changed files with 26 additions and 14 deletions

View File

@ -1,5 +1,6 @@
//! Implement a concrete type to build channels.
use std::io;
use std::sync::Mutex;
use crate::{event::ChanMgrEventSender, Error};
@ -81,8 +82,13 @@ impl<R: Runtime> ChanBuilder<R> {
.record_attempt();
}
let map_ioe = |ioe: io::Error| Error::Io {
peer: *addr,
source: ioe.into(),
};
// Establish a TCP connection.
let stream = self.runtime.connect(addr).await?;
let stream = self.runtime.connect(addr).await.map_err(map_ioe)?;
{
self.event_sender
@ -95,10 +101,12 @@ impl<R: Runtime> ChanBuilder<R> {
let tls = self
.tls_connector
.negotiate_unvalidated(stream, "ignored")
.await?;
.await
.map_err(map_ioe)?;
let peer_cert = tls
.peer_certificate()?
.peer_certificate()
.map_err(map_ioe)?
.ok_or(Error::Internal("TLS connection with no peer certificate"))?;
{
@ -208,9 +216,11 @@ mod test {
async {
// relay-side: accept the channel
// (and pretend to know what we're doing).
let (mut con, addr) = lis.accept().await?;
let (mut con, addr) = lis.accept().await.expect("accept failed");
assert_eq!(client_addr, addr.ip());
crate::testing::answer_channel_req(&mut con).await?;
crate::testing::answer_channel_req(&mut con)
.await
.expect("answer failed");
Ok(con)
}
);

View File

@ -1,5 +1,6 @@
//! Declare error types for tor-chanmgr
use std::net::SocketAddr;
use std::sync::Arc;
use futures::task::SpawnError;
@ -25,9 +26,16 @@ pub enum Error {
#[error("Protocol error while opening a channel: {0}")]
Proto(#[from] tor_proto::Error),
/// A protocol error while making a channel
#[error("I/O error while opening a channel: {0}")]
Io(#[source] Arc<std::io::Error>),
/// Network IO error or TLS error
#[error("Network IO error, or TLS error, talking to {peer}")]
Io {
/// Who we were talking to
peer: SocketAddr,
/// What happened. Might be some TLS library error wrapped up in io::Error
#[source]
source: Arc<std::io::Error>,
},
/// Unable to spawn task
#[error("unable to spawn task")]
@ -55,9 +63,3 @@ impl<T> From<std::sync::PoisonError<T>> for Error {
Error::Internal("Thread failed while holding lock")
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Error {
Error::Io(Arc::new(e))
}
}