diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2019-04-05 16:00:09 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2019-04-05 16:28:05 +0200 |
| commit | 4d04e91cefd5b6b423cf54f1a8d205d2094931a0 (patch) | |
| tree | b20894603eef2275b4718aab1986c3164b15cbae | |
| parent | af0c809a6587a1b79483f80470f321042c95a359 (diff) | |
| download | mullvadvpn-4d04e91cefd5b6b423cf54f1a8d205d2094931a0.tar.xz mullvadvpn-4d04e91cefd5b6b423cf54f1a8d205d2094931a0.zip | |
Get rid of error-chain in talpid-openvpn-plugin
| -rw-r--r-- | Cargo.lock | 3 | ||||
| -rw-r--r-- | talpid-openvpn-plugin/Cargo.toml | 3 | ||||
| -rw-r--r-- | talpid-openvpn-plugin/src/lib.rs | 68 | ||||
| -rw-r--r-- | talpid-openvpn-plugin/src/processing.rs | 55 |
4 files changed, 64 insertions, 65 deletions
diff --git a/Cargo.lock b/Cargo.lock index 24e9f28f7b..e42c14056b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2046,13 +2046,14 @@ name = "talpid-openvpn-plugin" version = "0.1.0" dependencies = [ "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)", "jsonrpc-client-core 0.5.0 (git+https://github.com/mullvad/jsonrpc-client-rs?rev=68aac55b)", "jsonrpc-client-ipc 0.5.0 (git+https://github.com/mullvad/jsonrpc-client-rs?rev=68aac55b)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "openvpn-plugin 0.3.0 (git+https://github.com/mullvad/openvpn-plugin-rs?branch=auth-failed-event)", "talpid-ipc 0.1.0", + "talpid-types 0.1.0", "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/talpid-openvpn-plugin/Cargo.toml b/talpid-openvpn-plugin/Cargo.toml index caf1e5637b..439d10131e 100644 --- a/talpid-openvpn-plugin/Cargo.toml +++ b/talpid-openvpn-plugin/Cargo.toml @@ -17,7 +17,7 @@ edition = "2018" crate-type = ["cdylib"] [dependencies] -error-chain = "0.12" +err-derive = "0.1.5" log = "0.4" env_logger = "0.6" jsonrpc-client-core = { git = "https://github.com/mullvad/jsonrpc-client-rs", rev = "68aac55b" } @@ -28,6 +28,7 @@ futures = "0.1" openvpn-plugin = { git = "https://github.com/mullvad/openvpn-plugin-rs", branch = "auth-failed-event", features = ["serde", "log"] } talpid-ipc = { path = "../talpid-ipc" } +talpid-types = { path = "../talpid-types" } [target.'cfg(windows)'.build-dependencies] diff --git a/talpid-openvpn-plugin/src/lib.rs b/talpid-openvpn-plugin/src/lib.rs index 34ddce52bb..fab5ce4ba2 100644 --- a/talpid-openvpn-plugin/src/lib.rs +++ b/talpid-openvpn-plugin/src/lib.rs @@ -8,36 +8,36 @@ #![deny(rust_2018_idioms)] -#[macro_use] -extern crate error_chain; - -use error_chain::ChainedError; use openvpn_plugin::{openvpn_plugin, EventResult, EventType}; -use std::{collections::HashMap, ffi::CString, sync::Mutex}; - +use std::{collections::HashMap, ffi::CString, io, sync::Mutex}; +use talpid_types::ErrorExt; mod processing; use crate::processing::EventProcessor; -error_chain! { - errors { - InitHandleFailed { - description("Unable to initialize event processor") - } - InvalidEventType { - description("Invalid event type constant") - } - ParseEnvFailed { - description("Unable to parse environment variables from OpenVPN") - } - ParseArgsFailed { - description("Unable to parse arguments from OpenVPN") - } - EventProcessingFailed { - description("Failed to process the event") - } - } +#[derive(err_derive::Error, Debug)] +pub enum Error { + #[error(display = "No core server id given as first argument")] + MissingCoreServerId, + + #[error(display = "Failed to sending an event to daemon over the IPC channel")] + SendEvent(#[error(cause)] jsonrpc_client_core::Error), + + #[error(display = "Connection is shut down")] + Shutdown, + + #[error(display = "Unable to start Tokio runtime")] + CreateRuntime(#[error(cause)] io::Error), + + #[error(display = "Unable to create IPC transport")] + CreateTransport(#[error(cause)] io::Error), + + #[error(display = "Unable to parse environment variables from OpenVPN")] + ParseEnvFailed(#[error(cause)] std::str::Utf8Error), + + #[error(display = "Unable to parse arguments from OpenVPN")] + ParseArgsFailed(#[error(cause)] std::str::Utf8Error), } @@ -63,7 +63,7 @@ pub struct Arguments { fn openvpn_open( args: Vec<CString>, _env: HashMap<CString, CString>, -) -> Result<(Vec<EventType>, Mutex<EventProcessor>)> { +) -> Result<(Vec<EventType>, Mutex<EventProcessor>), Error> { env_logger::init(); log::debug!("Initializing plugin"); @@ -72,20 +72,18 @@ fn openvpn_open( "Connecting back to talpid core at {}", arguments.ipc_socket_path ); - let processor = EventProcessor::new(arguments).chain_err(|| ErrorKind::InitHandleFailed)?; + let processor = EventProcessor::new(arguments)?; Ok((INTERESTING_EVENTS.to_vec(), Mutex::new(processor))) } -fn parse_args(args: &[CString]) -> Result<Arguments> { +fn parse_args(args: &[CString]) -> Result<Arguments, Error> { let mut args_iter = openvpn_plugin::ffi::parse::string_array_utf8(args) - .chain_err(|| ErrorKind::ParseArgsFailed)? + .map_err(Error::ParseArgsFailed)? .into_iter(); let _plugin_path = args_iter.next(); - let ipc_socket_path: String = args_iter - .next() - .ok_or_else(|| ErrorKind::Msg("No core server id given as first argument".to_owned()))?; + let ipc_socket_path: String = args_iter.next().ok_or_else(|| Error::MissingCoreServerId)?; Ok(Arguments { ipc_socket_path }) } @@ -100,17 +98,15 @@ fn openvpn_event( _args: Vec<CString>, env: HashMap<CString, CString>, handle: &mut Mutex<EventProcessor>, -) -> Result<EventResult> { +) -> Result<EventResult, Error> { log::debug!("Received event: {:?}", event); - let parsed_env = - openvpn_plugin::ffi::parse::env_utf8(&env).chain_err(|| ErrorKind::ParseEnvFailed)?; + let parsed_env = openvpn_plugin::ffi::parse::env_utf8(&env).map_err(Error::ParseEnvFailed)?; let result = handle .lock() .expect("failed to obtain mutex for EventProcessor") - .process_event(event, parsed_env) - .chain_err(|| ErrorKind::EventProcessingFailed); + .process_event(event, parsed_env); match result { Ok(()) => Ok(EventResult::Success), Err(e) => { diff --git a/talpid-openvpn-plugin/src/processing.rs b/talpid-openvpn-plugin/src/processing.rs index ab615d4d44..4124b6491c 100644 --- a/talpid-openvpn-plugin/src/processing.rs +++ b/talpid-openvpn-plugin/src/processing.rs @@ -1,4 +1,4 @@ -use super::Arguments; +use super::{Arguments, Error}; use jsonrpc_client_core::{ expand_params, jsonrpc_client, Future, Result as ClientResult, Transport, }; @@ -6,19 +6,6 @@ use jsonrpc_client_ipc::IpcTransport; use std::{collections::HashMap, sync::mpsc, thread}; use tokio::{reactor::Handle, runtime::Runtime}; -error_chain! { - errors { - IpcSendingError { - description("Failed while sending an event over the IPC channel") - } - - Shutdown { - description("Connection is shut down") - } - - } -} - /// Struct processing OpenVPN events and notifies listeners over IPC pub struct EventProcessor { @@ -27,23 +14,37 @@ pub struct EventProcessor { } impl EventProcessor { - pub fn new(arguments: Arguments) -> Result<EventProcessor> { + pub fn new(arguments: Arguments) -> Result<EventProcessor, Error> { log::trace!("Creating EventProcessor"); let (start_tx, start_rx) = mpsc::channel(); let (client_result_tx, client_result_rx) = mpsc::channel(); thread::spawn(move || { - let mut rt = Runtime::new().expect("failed to spawn runtime"); - + let mut rt = match Runtime::new().map_err(Error::CreateRuntime) { + Err(e) => { + let _ = start_tx.send(Err(e)); + return; + } + Ok(rt) => rt, + }; let (client, client_handle) = - IpcTransport::new(&arguments.ipc_socket_path, &Handle::default()) - .expect("Unable to create IPC transport") - .into_client(); + match IpcTransport::new(&arguments.ipc_socket_path, &Handle::default()) + .map_err(Error::CreateTransport) + .map(|transport| transport.into_client()) + { + Err(e) => { + let _ = start_tx.send(Err(e)); + return; + } + Ok((client, client_handle)) => (client, client_handle), + }; - let _ = start_tx.send(client_handle); + let _ = start_tx.send(Ok(client_handle)); let _ = client_result_tx.send(rt.block_on(client)); }); - let client_handle = start_rx.recv().chain_err(|| ErrorKind::Shutdown)?; + let client_handle = start_rx + .recv() + .expect("No start result from EventProcessor thread")?; let ipc_client = EventProxy::new(client_handle); Ok(EventProcessor { @@ -56,22 +57,22 @@ impl EventProcessor { &mut self, event: openvpn_plugin::EventType, env: HashMap<String, String>, - ) -> Result<()> { + ) -> Result<(), Error> { log::trace!("Processing \"{:?}\" event", event); let call_future = self .ipc_client .openvpn_event(event, env) - .map_err(|e| Error::with_chain(e, ErrorKind::IpcSendingError)); + .map_err(Error::SendEvent); call_future.wait()?; self.check_client_status() } - fn check_client_status(&mut self) -> Result<()> { + fn check_client_status(&mut self) -> Result<(), Error> { use std::sync::mpsc::TryRecvError::*; match self.client_result_rx.try_recv() { Err(Empty) => Ok(()), - Err(Disconnected) | Ok(Ok(())) => Err(ErrorKind::Shutdown.into()), - Ok(Err(e)) => Err(Error::with_chain(e, ErrorKind::IpcSendingError)), + Err(Disconnected) | Ok(Ok(())) => Err(Error::Shutdown), + Ok(Err(e)) => Err(Error::SendEvent(e)), } } } |
