diff options
author | Teddy Wing | 2017-05-09 01:21:57 +0200 |
---|---|---|
committer | Teddy Wing | 2017-05-09 01:21:57 +0200 |
commit | f0d5da113c59f364ffea3e840a9e871882478051 (patch) | |
tree | 47dcfec5a2577bcbf3152cd0c129daa13981233e | |
parent | 0fe722bbc2e53278dad2bab9268a3cca52e19825 (diff) | |
download | HearURL-f0d5da113c59f364ffea3e840a9e871882478051.tar.bz2 |
open_stream(): Don't terminate the TCP listener on error
Previously, errors would be returned immediately by the `?`/`try!`s.
Handle errors directly instead of returning them. Otherwise, the stream
listener would terminate, exiting the program, and forcing users to
restart it in order to restore functionality.
-rw-r--r-- | src/main.rs | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs index 40ff310..fcaca7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ use getopts::Options; use url::Url; use std::env; -use std::error::Error; use std::io::{self, Write}; use std::io::prelude::*; use std::net::TcpListener; @@ -13,7 +12,7 @@ use std::process::Command; const DEFAULT_PORT: u16 = 37705; -fn open_stream(browser: String, port: u16) -> Result<(), Box<Error>> { +fn open_stream(browser: String, port: u16) -> io::Result<()> { let listener = TcpListener::bind( format!("127.0.0.1:{}", port) )?; @@ -22,15 +21,24 @@ fn open_stream(browser: String, port: u16) -> Result<(), Box<Error>> { match stream { Ok(mut stream) => { let mut url = String::new(); - stream.read_to_string(&mut url)?; - - let url = Url::parse(url.as_str())?; - - Command::new("open") - .arg("-a") - .arg(&browser) - .arg(&url.as_str()) - .spawn()?; + match stream.read_to_string(&mut url) { + Ok(_) => {}, + Err(e) => writeln!(io::stderr(), "{}", e)?, + }; + + match Url::parse(url.as_str()) { + Ok(url) => { + match Command::new("open") + .arg("-a") + .arg(&browser) + .arg(&url.as_str()) + .spawn() { + Ok(_) => {}, + Err(e) => writeln!(io::stderr(), "{}", e)?, + }; + }, + Err(e) => writeln!(io::stderr(), "{}", e)?, + }; } Err(e) => { writeln!(io::stderr(), "{}", e)?; |