summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2019-04-11 16:41:15 +0200
committerLinus Färnstrand <linus@mullvad.net>2019-04-15 15:17:28 +0200
commit2fa4fc319fbe5271c733bb0ff1180efc73d55c2b (patch)
tree2b5921aeb53753e6c0c96b1f165523a7739b1a03
parentb7fa77843763122e5f0355d606e265e23e3a97b6 (diff)
downloadmullvadvpn-2fa4fc319fbe5271c733bb0ff1180efc73d55c2b.tar.xz
mullvadvpn-2fa4fc319fbe5271c733bb0ff1180efc73d55c2b.zip
Convert errors in CLI away from error-chain
-rw-r--r--Cargo.lock2
-rw-r--r--mullvad-cli/Cargo.toml2
-rw-r--r--mullvad-cli/src/cmds/relay.rs10
-rw-r--r--mullvad-cli/src/cmds/status.rs6
-rw-r--r--mullvad-cli/src/main.rs48
-rw-r--r--mullvad-ipc-client/src/lib.rs1
6 files changed, 40 insertions, 29 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 615b812eac..f46c8e5333 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1066,7 +1066,7 @@ dependencies = [
"base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "err-derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
"mullvad-ipc-client 0.1.0",
"mullvad-paths 0.1.0",
diff --git a/mullvad-cli/Cargo.toml b/mullvad-cli/Cargo.toml
index fa72874eb8..216d66489f 100644
--- a/mullvad-cli/Cargo.toml
+++ b/mullvad-cli/Cargo.toml
@@ -19,7 +19,7 @@ path = "src/main.rs"
[dependencies]
clap = "2.32"
-error-chain = "0.12"
+err-derive = "0.1.5"
env_logger = "0.6"
serde = "1.0"
futures = "0.1"
diff --git a/mullvad-cli/src/cmds/relay.rs b/mullvad-cli/src/cmds/relay.rs
index 33783d309f..7a202e4a5d 100644
--- a/mullvad-cli/src/cmds/relay.rs
+++ b/mullvad-cli/src/cmds/relay.rs
@@ -1,4 +1,4 @@
-use crate::{new_rpc_client, Command, Result, ResultExt};
+use crate::{new_rpc_client, Command, Error, Result};
use clap::{value_t, values_t};
use std::{
io::{self, BufRead},
@@ -335,7 +335,7 @@ impl Relay {
match vpn_protocol {
"wireguard" => {
if let Constraint::Only(TransportProtocol::Tcp) = protocol {
- return Err("WireGuard does not support TCP".into());
+ return Err(Error::InvalidCommand("WireGuard does not support TCP"));
}
self.update_constraints(RelaySettingsUpdate::Normal(RelayConstraintsUpdate {
location: None,
@@ -393,9 +393,9 @@ impl Relay {
fn parse_port_constraint(raw_port: &str) -> Result<Constraint<u16>> {
match raw_port.to_lowercase().as_str() {
"any" => Ok(Constraint::Any),
- port => Ok(Constraint::Only(
- u16::from_str(port).chain_err(|| "Invalid port")?,
- )),
+ port => Ok(Constraint::Only(u16::from_str(port).map_err(|_| {
+ Error::InvalidCommand("Invalid port. Must be \"any\" or [0-65535].")
+ })?)),
}
}
diff --git a/mullvad-cli/src/cmds/status.rs b/mullvad-cli/src/cmds/status.rs
index 9310707825..24adfb25f8 100644
--- a/mullvad-cli/src/cmds/status.rs
+++ b/mullvad-cli/src/cmds/status.rs
@@ -1,4 +1,4 @@
-use crate::{new_rpc_client, Command, Error, ErrorKind, Result, ResultExt};
+use crate::{new_rpc_client, Command, Error, Result};
use futures::{Future, Stream};
use mullvad_ipc_client::DaemonRpcClient;
use mullvad_types::{auth_failed::AuthFailed, DaemonEvent};
@@ -36,9 +36,9 @@ impl Command for Status {
let subscription = rpc
.daemon_event_subscribe()
.wait()
- .map_err(|_err| Error::from(ErrorKind::CantSubscribe))?;
+ .map_err(Error::CantSubscribe)?;
for event in subscription.wait() {
- match event.chain_err(|| "Subscription failed")? {
+ match event? {
DaemonEvent::StateTransition(new_state) => {
print_state(&new_state);
use self::TunnelStateTransition::*;
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();
diff --git a/mullvad-ipc-client/src/lib.rs b/mullvad-ipc-client/src/lib.rs
index 22c0b0d707..1c7433a6f9 100644
--- a/mullvad-ipc-client/src/lib.rs
+++ b/mullvad-ipc-client/src/lib.rs
@@ -23,6 +23,7 @@ static NO_ARGS: [u8; 0] = [];
pub type Result<T> = std::result::Result<T, jsonrpc_client_core::Error>;
pub use jsonrpc_client_core::Error;
+pub use jsonrpc_client_pubsub::Error as PubSubError;
pub fn new_standalone_ipc_client(path: &impl AsRef<Path>) -> io::Result<DaemonRpcClient> {
let path = path.as_ref().to_string_lossy().to_string();