summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2019-04-05 16:27:29 +0200
committerLinus Färnstrand <linus@mullvad.net>2019-04-05 16:27:29 +0200
commit95eaba22e7cc2f4e7fcf2717258486244e320aa3 (patch)
treef9f1719c524210039840bd1a3d74f88a6dd73853
parent5a710f1291b438ad42eeb447d182fb9c47d88fd2 (diff)
parent43b4cf61d1cf0b43e019ed7f9df8e00eec6d88b6 (diff)
downloadmullvadvpn-95eaba22e7cc2f4e7fcf2717258486244e320aa3.tar.xz
mullvadvpn-95eaba22e7cc2f4e7fcf2717258486244e320aa3.zip
Merge branch 'eliminate-error-chain-offline'
-rw-r--r--talpid-core/src/offline/dummy.rs5
-rw-r--r--talpid-core/src/offline/linux.rs81
-rw-r--r--talpid-core/src/offline/macos.rs15
-rw-r--r--talpid-core/src/offline/windows.rs19
4 files changed, 61 insertions, 59 deletions
diff --git a/talpid-core/src/offline/dummy.rs b/talpid-core/src/offline/dummy.rs
index e36ad8c4ee..7e7b215c72 100644
--- a/talpid-core/src/offline/dummy.rs
+++ b/talpid-core/src/offline/dummy.rs
@@ -1,11 +1,12 @@
use crate::tunnel_state_machine::TunnelCommand;
use futures::sync::mpsc::UnboundedSender;
-error_chain! {}
+#[derive(err_derive::Error, Debug)]
+pub struct Error(());
pub struct MonitorHandle;
-pub fn spawn_monitor(_sender: UnboundedSender<TunnelCommand>) -> Result<MonitorHandle> {
+pub fn spawn_monitor(_sender: UnboundedSender<TunnelCommand>) -> Result<MonitorHandle, Error> {
Ok(MonitorHandle)
}
diff --git a/talpid-core/src/offline/linux.rs b/talpid-core/src/offline/linux.rs
index 8adff502ca..c06f9f6bbd 100644
--- a/talpid-core/src/offline/linux.rs
+++ b/talpid-core/src/offline/linux.rs
@@ -1,30 +1,34 @@
use crate::tunnel_state_machine::TunnelCommand;
-use error_chain::ChainedError;
use futures::{future::Either, sync::mpsc::UnboundedSender, Future, Stream};
use iproute2::{Address, Connection, ConnectionHandle, Link, NetlinkIpError};
use log::{error, warn};
use netlink_socket::{Protocol, SocketAddr, TokioSocket};
use rtnetlink::{LinkLayerType, NetlinkCodec, NetlinkFramed, NetlinkMessage};
-use std::{collections::BTreeSet, thread};
+use std::{collections::BTreeSet, io, thread};
+use talpid_types::ErrorExt;
-error_chain! {
- errors {
- GetLinksError {
- description("Failed to get list of IP links")
- }
- NetlinkConnectionError {
- description("Failed to connect to netlink socket")
- }
- NetlinkBindError {
- description("Failed to start listening on netlink socket")
- }
- NetlinkError {
- description("Error while communicating on the netlink socket")
- }
- NetlinkDisconnected {
- description("Netlink connection has unexpectedly disconnected")
- }
- }
+
+pub type Result<T> = std::result::Result<T, Error>;
+
+#[derive(err_derive::Error, Debug)]
+pub enum Error {
+ #[error(display = "Failed to get list of IP links")]
+ GetLinksError(#[error(cause)] failure::Compat<iproute2::NetlinkIpError>),
+
+ #[error(display = "Failed to connect to netlink socket")]
+ NetlinkConnectionError(#[error(cause)] io::Error),
+
+ #[error(display = "Failed to start listening on netlink socket")]
+ NetlinkBindError(#[error(cause)] io::Error),
+
+ #[error(display = "Error while communicating on the netlink socket")]
+ NetlinkError(#[error(cause)] io::Error),
+
+ #[error(display = "Error while processing netlink messages")]
+ MonitorNetlinkError(#[error(cause)] failure::Compat<rtnetlink::Error>),
+
+ #[error(display = "Netlink connection has unexpectedly disconnected")]
+ NetlinkDisconnected,
}
const RTMGRP_NOTIFY: u32 = 1;
@@ -35,22 +39,23 @@ const RTMGRP_IPV6_IFADDR: u32 = 0x100;
pub struct MonitorHandle;
pub fn spawn_monitor(sender: UnboundedSender<TunnelCommand>) -> Result<MonitorHandle> {
- let mut socket =
- TokioSocket::new(Protocol::Route).chain_err(|| ErrorKind::NetlinkConnectionError)?;
+ let mut socket = TokioSocket::new(Protocol::Route).map_err(Error::NetlinkConnectionError)?;
socket
.bind(&SocketAddr::new(
0,
RTMGRP_NOTIFY | RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR,
))
- .chain_err(|| ErrorKind::NetlinkBindError)?;
+ .map_err(Error::NetlinkBindError)?;
let channel = NetlinkFramed::new(socket, NetlinkCodec::<NetlinkMessage>::new());
let link_monitor = LinkMonitor::new(sender);
thread::spawn(|| {
if let Err(error) = monitor_event_loop(channel, link_monitor) {
- let chained_error = error.chain_err(|| "Error running link monitor event loop");
- error!("{}", chained_error.display_chain());
+ error!(
+ "{}",
+ error.display_chain_with_msg("Error running link monitor event loop")
+ );
}
});
@@ -59,8 +64,10 @@ pub fn spawn_monitor(sender: UnboundedSender<TunnelCommand>) -> Result<MonitorHa
pub fn is_offline() -> bool {
check_if_offline().unwrap_or_else(|error| {
- let chained_error = error.chain_err(|| "Failed to check for internet connection");
- warn!("{}", chained_error.display_chain());
+ warn!(
+ "{}",
+ error.display_chain_with_msg("Failed to check for internet connection")
+ );
false
})
}
@@ -91,7 +98,7 @@ impl NetlinkConnection {
/// Open a connection on the netlink socket.
pub fn new() -> Result<Self> {
let (connection, connection_handle) =
- iproute2::new_connection().chain_err(|| ErrorKind::NetlinkConnectionError)?;
+ iproute2::new_connection().map_err(Error::NetlinkConnectionError)?;
Ok(NetlinkConnection {
connection: Some(connection),
@@ -125,20 +132,14 @@ impl NetlinkConnection {
where
R: Future<Error = NetlinkIpError>,
{
- let connection = self
- .connection
- .take()
- .ok_or(ErrorKind::NetlinkDisconnected)?;
+ let connection = self.connection.take().ok_or(Error::NetlinkDisconnected)?;
let (result, connection) = match connection.select2(request).wait() {
- Ok(Either::A(_)) => bail!(ErrorKind::NetlinkDisconnected),
- Err(Either::A((error, _))) => bail!(Error::with_chain(error, ErrorKind::NetlinkError)),
+ Ok(Either::A(_)) => return Err(Error::NetlinkDisconnected),
+ Err(Either::A((error, _))) => return Err(Error::NetlinkError(error)),
Ok(Either::B((links, connection))) => (Ok(links), connection),
Err(Either::B((error, connection))) => (
- Err(Error::with_chain(
- failure::Fail::compat(error),
- ErrorKind::GetLinksError,
- )),
+ Err(Error::GetLinksError(failure::Fail::compat(error))),
connection,
),
};
@@ -165,9 +166,7 @@ fn monitor_event_loop(
Ok(())
})
.wait()
- .map_err(|error| {
- Error::with_chain(failure::Fail::compat(error), ErrorKind::NetlinkError)
- })?;
+ .map_err(|error| Error::MonitorNetlinkError(failure::Fail::compat(error)))?;
Ok(())
}
diff --git a/talpid-core/src/offline/macos.rs b/talpid-core/src/offline/macos.rs
index 0153357f18..183dd0f09a 100644
--- a/talpid-core/src/offline/macos.rs
+++ b/talpid-core/src/offline/macos.rs
@@ -14,15 +14,16 @@ use system_configuration::{
const PRIMARY_INTERFACE_KEY: &str = "State:/Network/Global/IPv4";
-error_chain! {
- errors {
- DynamicStoreInitError { description("Failed to initialize dynamic store") }
- }
+
+#[derive(err_derive::Error, Debug)]
+pub enum Error {
+ #[error(display = "Failed to initialize dynamic store")]
+ DynamicStoreInitError,
}
pub struct MonitorHandle;
-pub fn spawn_monitor(sender: UnboundedSender<TunnelCommand>) -> Result<MonitorHandle> {
+pub fn spawn_monitor(sender: UnboundedSender<TunnelCommand>) -> Result<MonitorHandle, Error> {
let (result_tx, result_rx) = mpsc::channel();
thread::spawn(move || match create_dynamic_store(sender) {
Ok(store) => {
@@ -41,7 +42,7 @@ pub fn is_offline() -> bool {
is_offline
}
-fn create_dynamic_store(sender: UnboundedSender<TunnelCommand>) -> Result<SCDynamicStore> {
+fn create_dynamic_store(sender: UnboundedSender<TunnelCommand>) -> Result<SCDynamicStore, Error> {
let callback_context = SCDynamicStoreCallBackContext {
callout: primary_interface_change_callback,
info: sender,
@@ -58,7 +59,7 @@ fn create_dynamic_store(sender: UnboundedSender<TunnelCommand>) -> Result<SCDyna
trace!("Registered for dynamic store notifications");
Ok(store)
} else {
- bail!(ErrorKind::DynamicStoreInitError)
+ Err(Error::DynamicStoreInitError)
}
}
diff --git a/talpid-core/src/offline/windows.rs b/talpid-core/src/offline/windows.rs
index 5a385c61bf..e40d8fe44f 100644
--- a/talpid-core/src/offline/windows.rs
+++ b/talpid-core/src/offline/windows.rs
@@ -11,6 +11,7 @@ use futures::sync::mpsc::UnboundedSender;
use log::debug;
use std::{
ffi::c_void,
+ io,
mem::zeroed,
os::windows::io::{IntoRawHandle, RawHandle},
ptr, thread,
@@ -40,14 +41,14 @@ use winapi::{
const CLASS_NAME: &[u8] = b"S\0T\0A\0T\0I\0C\0\0\0";
const REQUEST_THREAD_SHUTDOWN: UINT = WM_USER + 1;
-error_chain! {
- errors {
- ThreadCreationError {
- description("Unable to create listener thread")
- }
- }
+
+#[derive(err_derive::Error, Debug)]
+pub enum Error {
+ #[error(display = "Unable to create listener thread")]
+ ThreadCreationError(#[error(cause)] io::Error),
}
+
pub struct BroadcastListener {
thread_handle: RawHandle,
thread_id: DWORD,
@@ -56,7 +57,7 @@ pub struct BroadcastListener {
unsafe impl Send for BroadcastListener {}
impl BroadcastListener {
- pub fn start<F>(client_callback: F) -> Result<Self>
+ pub fn start<F>(client_callback: F) -> Result<Self, Error>
where
F: Fn(UINT, WPARAM, LPARAM) + 'static + Send,
{
@@ -64,7 +65,7 @@ impl BroadcastListener {
.spawn(move || unsafe {
Self::message_pump(client_callback);
})
- .chain_err(|| ErrorKind::ThreadCreationError)?;
+ .map_err(Error::ThreadCreationError)?;
let real_handle = join_handle.into_raw_handle();
@@ -169,7 +170,7 @@ impl Drop for BroadcastListener {
pub type MonitorHandle = BroadcastListener;
-pub fn spawn_monitor(sender: UnboundedSender<TunnelCommand>) -> Result<MonitorHandle> {
+pub fn spawn_monitor(sender: UnboundedSender<TunnelCommand>) -> Result<MonitorHandle, Error> {
let listener =
BroadcastListener::start(move |message: UINT, wparam: WPARAM, _lparam: LPARAM| {
if message == WM_POWERBROADCAST {