From 2a471ffd1a1d70ed585067d2e1dc1bf94b8f3103 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 26 Jan 2022 15:44:12 +0000 Subject: [PATCH] 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. --- crates/tor-chanmgr/src/builder.rs | 20 +++++++++++++++----- crates/tor-chanmgr/src/err.rs | 20 +++++++++++--------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/crates/tor-chanmgr/src/builder.rs b/crates/tor-chanmgr/src/builder.rs index d9643da99..21f2374d9 100644 --- a/crates/tor-chanmgr/src/builder.rs +++ b/crates/tor-chanmgr/src/builder.rs @@ -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 ChanBuilder { .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 ChanBuilder { 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) } ); diff --git a/crates/tor-chanmgr/src/err.rs b/crates/tor-chanmgr/src/err.rs index 5ff9cf75c..b0bd0af91 100644 --- a/crates/tor-chanmgr/src/err.rs +++ b/crates/tor-chanmgr/src/err.rs @@ -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), + /// 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, + }, /// Unable to spawn task #[error("unable to spawn task")] @@ -55,9 +63,3 @@ impl From> for Error { Error::Internal("Thread failed while holding lock") } } - -impl From for Error { - fn from(e: std::io::Error) -> Error { - Error::Io(Arc::new(e)) - } -}