From 0b058f570e71a3eaa1314a947fdff3d88193a63f Mon Sep 17 00:00:00 2001 From: bnewbold Date: Thu, 2 Jun 2016 21:55:19 -0400 Subject: more improvements to error handling --- src/client.rs | 13 ++++++++----- src/common.rs | 6 +++--- 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/client.rs b/src/client.rs index acc3425..f26c35d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -39,15 +39,15 @@ pub fn run_client(host: &str, local_file: &str, remote_file: &str, remote_is_dir let ssh_output = ssh_cmd.output().expect("couldn't get SSH sub-process output"); if !ssh_output.status.success() { - println!("Error on remote end: {}", String::from_utf8_lossy(&ssh_output.stderr)); - exit(-1); + return Err(format!("Error on remote end: {}", + String::from_utf8_lossy(&ssh_output.stderr))); } let reply = String::from_utf8_lossy(&ssh_output.stdout); - println!("SSH reply: {}", reply); + //println!("SSH reply: {}", reply); let words: Vec<&str> = reply.split_whitespace().collect(); if words.len() != 7 || words[0] != "UCP" || words[1] != "CONNECT" { - panic!("Unexpected data via SSH pipe (TCP)"); + return Err("Unexpected data via SSH pipe (TCP)".to_string()); } let remote_host = words[2]; let remote_port = words[3].parse::().expect("failed to parse remote port number"); @@ -64,7 +64,10 @@ pub fn run_client(host: &str, local_file: &str, remote_file: &str, remote_is_dir let addr = net::IpAddr::from_str(remote_host).unwrap(); let mut socket = UdtSocket::new(udt::SocketFamily::AFInet, udt::SocketType::Stream).unwrap(); - socket.connect(net::SocketAddr::new(addr, remote_port)).unwrap();; + match socket.connect(net::SocketAddr::new(addr, remote_port)) { + Ok(_) => { println!("Connected."); }, + Err(e) => { return Err(e.err_msg); }, + }; let mut stream: UdtStream = UdtStream::new(socket); if !no_crypto { diff --git a/src/common.rs b/src/common.rs index 77b28ce..9aa477f 100644 --- a/src/common.rs +++ b/src/common.rs @@ -31,7 +31,7 @@ pub fn source_files(stream: &mut S, file_path: &str, recursive: 1 | 2 => { // Warning unimplemented!(); }, - _ => { panic!("Unexpected status char!") }, + _ => { return Err("Unexpected status char!".to_string()); }, }; let mut buf = [0; 4096]; @@ -52,7 +52,7 @@ pub fn source_files(stream: &mut S, file_path: &str, recursive: 1 | 2 => { // Warning unimplemented!(); }, - _ => { panic!("Unexpected status char!") }, + _ => { return Err("Unexpected status char!".to_string()) }, }; Ok(()) } @@ -89,7 +89,7 @@ pub fn sink_files(stream: &mut S, file_path: &str, recursive: b 'D' => { unimplemented!(); }, 'E' => { unimplemented!(); }, 'T' => { unimplemented!(); }, - _ => { panic!(format!("Unexpected message type: {}", msg_type)); }, + _ => { return Err(format!("Unexpected message type: {}", msg_type)); }, }; let line = raw_read_line(stream).unwrap(); println!("got msg: {}", line); -- cgit v1.2.3