From 2291cf25a351880d8d4017e1a5f6df259d59da73 Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Mon, 28 Oct 2024 15:51:31 +0100 Subject: [PATCH] improve error handling --- src/connect.rs | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/connect.rs b/src/connect.rs index 927c001..1214dad 100644 --- a/src/connect.rs +++ b/src/connect.rs @@ -36,7 +36,7 @@ pub(crate) enum LanMouseConnectionError { NotConnected, #[error("emulation is disabled on the target device")] TargetEmulationDisabled, - #[error("connection timed out")] + #[error("Connection timed out")] Timeout, } @@ -45,10 +45,14 @@ const DEFAULT_CONNECTION_TIMEOUT: Duration = Duration::from_secs(5); async fn connect( addr: SocketAddr, cert: Certificate, -) -> Result<(Arc, SocketAddr), LanMouseConnectionError> { +) -> Result<(Arc, SocketAddr), (SocketAddr, LanMouseConnectionError)> { log::info!("connecting to {addr} ..."); - let conn = Arc::new(UdpSocket::bind("0.0.0.0:0").await?); - conn.connect(addr).await?; + let conn = Arc::new( + UdpSocket::bind("0.0.0.0:0") + .await + .map_err(|e| (addr, e.into()))?, + ); + conn.connect(addr).await.map_err(|e| (addr, e.into()))?; let config = Config { certificates: vec![cert], server_name: "ignored".to_owned(), @@ -58,21 +62,10 @@ async fn connect( }; let timeout = tokio::time::sleep(DEFAULT_CONNECTION_TIMEOUT); tokio::select! { - _ = timeout => { - log::warn!("connection to {addr} timed out"); - Err(LanMouseConnectionError::Timeout) - } - result = DTLSConn::new(conn, config, true, None) => { - match result { - Ok(dtls_conn) => { - log::info!("{addr} connected successfully!"); - Ok((Arc::new(dtls_conn), addr)) - } - Err(e) => { - log::warn!("failed to connect to {addr}: {e}"); - Err(e.into()) - } - } + _ = timeout => Err((addr, LanMouseConnectionError::Timeout)), + result = DTLSConn::new(conn, config, true, None) => match result { + Ok(dtls_conn) => Ok((Arc::new(dtls_conn), addr)), + Err(e) => Err((addr, e.into())), } } } @@ -90,9 +83,8 @@ async fn connect_any( None => return Err(LanMouseConnectionError::NotConnected), Some(r) => match r.expect("join error") { Ok(conn) => return Ok(conn), - Err(e) => { - log::warn!("failed to connect: {e}"); - continue; + Err((a, e)) => { + log::warn!("failed to connect to {a}: `{e}`") } }, };