diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2019-04-11 16:41:15 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2019-04-15 15:17:28 +0200 |
| commit | 2fa4fc319fbe5271c733bb0ff1180efc73d55c2b (patch) | |
| tree | 2b5921aeb53753e6c0c96b1f165523a7739b1a03 /mullvad-cli/src/main.rs | |
| parent | b7fa77843763122e5f0355d606e265e23e3a97b6 (diff) | |
| download | mullvadvpn-2fa4fc319fbe5271c733bb0ff1180efc73d55c2b.tar.xz mullvadvpn-2fa4fc319fbe5271c733bb0ff1180efc73d55c2b.zip | |
Convert errors in CLI away from error-chain
Diffstat (limited to 'mullvad-cli/src/main.rs')
| -rw-r--r-- | mullvad-cli/src/main.rs | 48 |
1 files changed, 29 insertions, 19 deletions
diff --git a/mullvad-cli/src/main.rs b/mullvad-cli/src/main.rs index e4fde0c211..fcb8246cfd 100644 --- a/mullvad-cli/src/main.rs +++ b/mullvad-cli/src/main.rs @@ -8,9 +8,6 @@ #![deny(rust_2018_idioms)] -#[macro_use] -extern crate error_chain; - use clap::{crate_authors, crate_description, crate_name}; use mullvad_ipc_client::{new_standalone_ipc_client, DaemonRpcClient}; use std::io; @@ -20,34 +17,47 @@ mod cmds; pub const PRODUCT_VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/product-version.txt")); +pub type Result<T> = std::result::Result<T, Error>; -error_chain! { +#[derive(err_derive::Error, Debug)] +pub enum Error { + #[error(display = "Failed to connect to daemon")] + DaemonNotRunning(#[error(cause)] io::Error), - errors { - DaemonNotRunning(err: io::Error) { - description("Failed to connect to daemon") - display("Failed to connect to daemon: {}Is the daemon running?", err.display_chain()) - } - CantSubscribe { - description("Can't subscribe to daemon states") - } - } + #[error(display = "Can't subscribe to daemon states")] + CantSubscribe(#[error(cause)] mullvad_ipc_client::PubSubError), + + #[error(display = "Failed to communicate with mullvad-daemon over RPC")] + RpcClientError(#[error(cause)] mullvad_ipc_client::Error), - foreign_links { - Io(io::Error); - ParseIntError(::std::num::ParseIntError); - RpcClientError(mullvad_ipc_client::Error); + /// The given command is not correct in some way + #[error(display = "Invalid command: {}", _0)] + InvalidCommand(&'static str), +} + +impl From<mullvad_ipc_client::Error> for Error { + fn from(e: mullvad_ipc_client::Error) -> Self { + Error::RpcClientError(e) } } pub fn new_rpc_client() -> Result<DaemonRpcClient> { match new_standalone_ipc_client(&mullvad_paths::get_rpc_socket_path()) { - Err(e) => Err(ErrorKind::DaemonNotRunning(e).into()), + Err(e) => Err(Error::DaemonNotRunning(e)), Ok(client) => Ok(client), } } -quick_main!(run); +fn main() { + let exit_code = match run() { + Ok(_) => 0, + Err(error) => { + eprintln!("{}", error.display_chain()); + 1 + } + }; + std::process::exit(exit_code); +} fn run() -> Result<()> { env_logger::init(); |
