From c2daf7a11eed4b0f06d333479512056eb27671cc Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 6 Jul 2021 14:46:00 +1000 Subject: [PATCH] Make use of torut's errors implementing `std::error::Error` Anyhow all the things! --- swap/src/tor.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/swap/src/tor.rs b/swap/src/tor.rs index 4a6b3e3e..bbbcb16b 100644 --- a/swap/src/tor.rs +++ b/swap/src/tor.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, bail, Context, Result}; +use anyhow::{bail, Context, Result}; use std::future::Future; use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use tokio::net::TcpStream; @@ -50,7 +50,7 @@ impl Client { pub async fn assert_tor_running(&self) -> Result<()> { // Make sure you are running tor and this is your socks port let proxy = reqwest::Proxy::all(format!("socks5h://{}", self.socks5_address).as_str()) - .map_err(|_| anyhow!("tor proxy should be there"))?; + .context("Failed to construct Tor proxy URL")?; let client = reqwest::Client::builder().proxy(proxy).build()?; let res = client.get("https://check.torproject.org").send().await?; @@ -77,21 +77,21 @@ impl Client { let mut uc = self .init_unauthenticated_connection() .await - .map_err(|_| anyhow!("Could not connect to Tor. Tor might not be running or the control port is incorrect."))?; + .context("Failed to connect to Tor")?; let tor_info = uc .load_protocol_info() .await - .map_err(|_| anyhow!("Failed to load protocol info from Tor."))?; + .context("Failed to load protocol info from Tor")?; let tor_auth_data = tor_info .make_auth_data()? - .context("Failed to make Tor auth data.")?; + .context("Failed to make Tor auth data")?; // Get an authenticated connection to the Tor via the Tor Controller protocol. uc.authenticate(&tor_auth_data) .await - .map_err(|_| anyhow!("Failed to authenticate with Tor"))?; + .context("Failed to authenticate with Tor")?; Ok(AuthenticatedClient { inner: uc.into_authenticated().await, @@ -123,6 +123,6 @@ impl AuthenticatedClient { self.inner .add_onion_v3(tor_key, false, false, false, None, &mut listeners) .await - .map_err(|e| anyhow!("Could not add onion service.: {:#?}", e)) + .context("Failed to add onion service") } }