diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-04-23 17:21:52 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-05-03 08:23:30 -0300 |
| commit | 4bbbb3be8027292e7ad6d19fc04a567de438c4ca (patch) | |
| tree | c9d0df8970ef86a6e2e5207b3226b0dc281162d2 | |
| parent | ae6119b4e8c8526903a24a70a520db733f598afc (diff) | |
| download | mullvadvpn-4bbbb3be8027292e7ad6d19fc04a567de438c4ca.tar.xz mullvadvpn-4bbbb3be8027292e7ad6d19fc04a567de438c4ca.zip | |
Use error-chain to define custom error types
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | mullvad-daemon/tests/startup.rs | 2 | ||||
| -rw-r--r-- | mullvad-ipc-client/Cargo.toml | 1 | ||||
| -rw-r--r-- | mullvad-ipc-client/src/lib.rs | 76 |
4 files changed, 60 insertions, 20 deletions
diff --git a/Cargo.lock b/Cargo.lock index 28c2ea5117..c45299eb15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -742,6 +742,7 @@ dependencies = [ name = "mullvad-ipc-client" version = "0.1.0" dependencies = [ + "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "talpid-ipc 0.1.0", ] diff --git a/mullvad-daemon/tests/startup.rs b/mullvad-daemon/tests/startup.rs index 51a24c0577..93631335dc 100644 --- a/mullvad-daemon/tests/startup.rs +++ b/mullvad-daemon/tests/startup.rs @@ -20,7 +20,7 @@ use platform_specific::*; #[cfg(not(windows))] #[test] fn rpc_info_file_permissions() { - let rpc_file = rpc_file_path(); + let rpc_file = rpc_file_path().unwrap(); if let Err(error) = fs::remove_file(&rpc_file) { if error.kind() != io::ErrorKind::NotFound { diff --git a/mullvad-ipc-client/Cargo.toml b/mullvad-ipc-client/Cargo.toml index b6018ed774..5a26c323ea 100644 --- a/mullvad-ipc-client/Cargo.toml +++ b/mullvad-ipc-client/Cargo.toml @@ -6,5 +6,6 @@ description = "RPC client for Mullvad daemon" license = "GPL-3.0" [dependencies] +error-chain = "0.11" serde = "1.0" talpid-ipc = { path = "../talpid-ipc" } diff --git a/mullvad-ipc-client/src/lib.rs b/mullvad-ipc-client/src/lib.rs index 22704693e8..4167262ef1 100644 --- a/mullvad-ipc-client/src/lib.rs +++ b/mullvad-ipc-client/src/lib.rs @@ -1,3 +1,5 @@ +#[macro_use] +extern crate error_chain; extern crate serde; extern crate talpid_ipc; @@ -6,56 +8,92 @@ use std::io::{BufRead, BufReader}; use std::path::PathBuf; use serde::{Deserialize, Serialize}; - use talpid_ipc::WsIpcClient; +error_chain! { + errors { + EmptyRpcFile(file_path: String) { + description("RPC connection file is empty") + display("RPC connection file \"{}\" is empty", file_path) + } + + ReadRpcFileError(file_path: String) { + description("Failed to read RPC connection information") + display("Failed to read RPC connection information from {}", file_path) + } + + RpcCallError(method: String) { + description("Failed to call RPC method") + display("Failed to call RPC method \"{}\"", method) + } + + StartRpcClient(address: String) { + description("Failed to start RPC client") + display("Failed to start RPC client to {}", address) + } + + UnknownRpcFilePath { + description("Failed to determine RPC connection information file path") + } + } +} + pub struct DaemonRpcClient { address: String, } impl DaemonRpcClient { - pub fn new() -> Result<Self, String> { - let rpc_file = File::open(rpc_file_path()) - .map_err(|error| format!("failed to open RPC address file: {}", error))?; + pub fn new() -> Result<Self> { + let address = Self::read_rpc_file()?; + + Ok(DaemonRpcClient { address }) + } + + fn read_rpc_file() -> Result<String> { + let file_path = rpc_file_path()?; + let file_path_string = || file_path.display().to_string(); + let rpc_file = + File::open(&file_path).chain_err(|| ErrorKind::ReadRpcFileError(file_path_string()))?; let reader = BufReader::new(rpc_file); let mut lines = reader.lines(); - let address = lines - .next() - .ok_or("RPC address file is empty".to_string())? - .map_err(|error| format!("failed to read address from RPC address file: {}", error))?; - Ok(DaemonRpcClient { address }) + lines + .next() + .ok_or_else(|| ErrorKind::EmptyRpcFile(file_path_string()))? + .chain_err(|| ErrorKind::ReadRpcFileError(file_path_string())) } - pub fn shutdown(&self) -> Result<(), String> { + pub fn shutdown(&self) -> Result<()> { self.call("shutdown", &[] as &[u8; 0]) } - pub fn call<A, O>(&self, method: &str, args: &A) -> Result<O, String> + pub fn call<A, O>(&self, method: &str, args: &A) -> Result<O> where A: Serialize, O: for<'de> Deserialize<'de>, { let mut rpc_client = WsIpcClient::new(self.address.clone()) - .map_err(|error| format!("unable to create RPC client: {}", error))?; + .chain_err(|| ErrorKind::StartRpcClient(self.address.clone()))?; rpc_client .call(method, args) - .map_err(|error| format!("RPC request failed: {}", error)) + .chain_err(|| ErrorKind::RpcCallError(method.to_owned())) } } #[cfg(unix)] -pub fn rpc_file_path() -> PathBuf { +pub fn rpc_file_path() -> Result<PathBuf> { use std::path::Path; - Path::new("/tmp/.mullvad_rpc_address").to_path_buf() + Ok(Path::new("/tmp/.mullvad_rpc_address").to_path_buf()) } #[cfg(windows)] -pub fn rpc_file_path() -> PathBuf { - let windows_directory = ::std::env::var_os("WINDIR").unwrap(); - PathBuf::from(windows_directory) +pub fn rpc_file_path() -> Result<PathBuf> { + let windows_directory = + ::std::env::var_os("WINDIR").ok_or_else(|| ErrorKind::UnknownRpcFilePath)?; + + Ok(PathBuf::from(windows_directory) .join("Temp") - .join(".mullvad_rpc_address") + .join(".mullvad_rpc_address")) } |
