diff --git a/src/common.rs b/src/common.rs index f3beeea..fc98596 100644 --- a/src/common.rs +++ b/src/common.rs @@ -15,7 +15,7 @@ const PUBLIC_IPV6: &str = "https://ipv6.icanhazip.com"; #[derive(Debug, PartialEq, Eq)] pub enum Message { - ErrorMsg(String), + Error(String), Files(Vec), Metadata(HashMap), ClientConnect(SocketAddr), diff --git a/src/comms.rs b/src/comms.rs index 236dc6e..b772890 100755 --- a/src/comms.rs +++ b/src/comms.rs @@ -41,7 +41,7 @@ pub async fn recv( let n = reader.read_until(b':', &mut buf).await?; if n == 0 { - todo!("maybe error handling :)"); + return Err("Received 0 bytes from the socket".into()); } buf.pop(); diff --git a/src/connector.rs b/src/connector.rs index 01ff126..da21784 100755 --- a/src/connector.rs +++ b/src/connector.rs @@ -83,7 +83,7 @@ impl Connector { let msg = String::from_utf8(buf)?; if msg == "FIN" { - todo!("maybe error handling :)"); + return Err("Incorrect access key".into()); } Ok(()) diff --git a/src/listener.rs b/src/listener.rs index 7fc3899..f3edcef 100755 --- a/src/listener.rs +++ b/src/listener.rs @@ -55,15 +55,23 @@ impl Listener { let files = files.clone(); let (mut socket, addr) = listener.accept().await?; tx.send(Message::ClientConnect(addr)).await?; - let this_tx = tx.clone(); - let this_self = Arc::clone(&self); + let conn_tx = tx.clone(); + let err_tx = tx.clone(); + let conn_self = Arc::clone(&self); - tokio::spawn(async move { - this_self - .connection(&mut socket, addr, this_tx, &files) - .await?; - Ok::<(), Box>(()) - }); + match tokio::spawn(async move { + conn_self + .connection(&mut socket, addr, conn_tx, &files) + .await + }) + .await + { + Ok(_) => {} + Err(e) => { + let err_msg = format!("{}: {}", addr, e); + err_tx.send(Message::Error(err_msg)).await?; + } + }; } } @@ -162,7 +170,7 @@ impl Listener { let msg = String::from_utf8(buf)?; if msg != "AMT" { - todo!("maybe error handling :)"); + return Err("Broken message sequence".into()); } for file in metadata { @@ -217,7 +225,7 @@ impl Listener { let msg = String::from_utf8(buf)?; if msg == "ERROR" { - todo!("maybe error handling :)"); + return Err("Incomplete file request (hashes don't match)".into()); } }