From f0d5da113c59f364ffea3e840a9e871882478051 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 9 May 2017 01:21:57 +0200 Subject: 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. --- src/main.rs | 30 +++++++++++++++++++----------- 1 file 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> { +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> { 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)?; -- cgit v1.2.3