Use ? error handling in subcommands

This commit is contained in:
Tangent 128 2018-04-11 19:45:02 -04:00
parent 97359801c2
commit 9dbd59c313
2 changed files with 9 additions and 6 deletions

View file

@ -1,3 +1,4 @@
use std::error::Error;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
use std::sync::{ use std::sync::{
@ -117,11 +118,12 @@ pub fn options() -> App<'static, 'static> {
.required(true)) .required(true))
} }
pub fn run(args: &ArgMatches) { pub fn run(args: &ArgMatches) -> Result<(), Box<Error>> {
let single_channel = Channel::new(); let single_channel = Channel::new();
let addr_str = args.value_of("listen").unwrap(); let addr_str = args.value_of("listen").ok_or("Listen address wasn't provided")?;
let addr = addr_str.to_socket_addrs().expect("parsing bind address").next().expect("resolving bind address"); let addr = addr_str.to_socket_addrs()?.next().ok_or("Listen address didn't resolve")?;
Http::new().bind(&addr, move || Ok(RelayServer(single_channel.clone()))).unwrap().run().unwrap(); Http::new().bind(&addr, move || Ok(RelayServer(single_channel.clone())))?.run()?;
Ok(())
} }

View file

@ -23,7 +23,8 @@ fn main() {
("relay", Some(sub_args)) => relay::run(sub_args), ("relay", Some(sub_args)) => relay::run(sub_args),
_ => { _ => {
options().print_help().unwrap(); options().print_help().unwrap();
println!("") println!("");
} Ok(())
} }
}.unwrap_or_else(|err| println!("Error: {}", err));
} }