summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2019-04-05 15:11:51 +0200
committerLinus Färnstrand <linus@mullvad.net>2019-04-05 16:28:05 +0200
commit7cacecc0056607f4872377137ba00965348179c1 (patch)
tree98f95a18f11dd3d29da81b4cabddc2ceea9ab248
parent686ae61bc224c0fc62f7087df4a343dcaebf6664 (diff)
downloadmullvadvpn-7cacecc0056607f4872377137ba00965348179c1.tar.xz
mullvadvpn-7cacecc0056607f4872377137ba00965348179c1.zip
Remove error-chain from static_resolv_conf DNS manager
-rw-r--r--talpid-core/src/dns/linux/static_resolv_conf.rs73
1 files changed, 36 insertions, 37 deletions
diff --git a/talpid-core/src/dns/linux/static_resolv_conf.rs b/talpid-core/src/dns/linux/static_resolv_conf.rs
index 71b6973edf..73c9aa4221 100644
--- a/talpid-core/src/dns/linux/static_resolv_conf.rs
+++ b/talpid-core/src/dns/linux/static_resolv_conf.rs
@@ -1,5 +1,4 @@
use super::RESOLV_CONF_PATH;
-use error_chain::ChainedError;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use parking_lot::Mutex;
use resolv_conf::{Config, ScopedIp};
@@ -9,27 +8,28 @@ use std::{
sync::{mpsc, Arc},
thread,
};
+use talpid_types::ErrorExt;
const RESOLV_CONF_BACKUP_PATH: &str = "/etc/resolv.conf.mullvadbackup";
-error_chain! {
- errors {
- WatchResolvConf {
- description("Failed to watch /etc/resolv.conf for changes")
- }
+pub type Result<T> = std::result::Result<T, Error>;
- WriteResolvConf {
- description("Failed to write to /etc/resolv.conf")
- }
+#[derive(err_derive::Error, Debug)]
+pub enum Error {
+ #[error(display = "Failed to watch /etc/resolv.conf for changes")]
+ WatchResolvConf(#[error(cause)] notify::Error),
- BackupResolvConf {
- description("Failed to create backup of /etc/resolv.conf")
- }
+ #[error(display = "Failed to write to {}", _0)]
+ WriteResolvConf(&'static str, #[error(cause)] io::Error),
- RestoreResolvConf {
- description("Failed to restore /etc/resolv.conf from backup")
- }
- }
+ #[error(display = "Failed to read from {}", _0)]
+ ReadResolvConf(&'static str, #[error(cause)] io::Error),
+
+ #[error(display = "resolv.conf at {} could not be parsed", _0)]
+ ParseError(&'static str, #[error(cause)] resolv_conf::ParseError),
+
+ #[error(display = "Failed to remove stale resolv.conf backup at {}", _0)]
+ RemoveBackup(&'static str, #[error(cause)] io::Error),
}
pub struct StaticResolvConf {
@@ -39,7 +39,7 @@ pub struct StaticResolvConf {
impl StaticResolvConf {
pub fn new() -> Result<Self> {
- restore_from_backup().chain_err(|| ErrorKind::RestoreResolvConf)?;
+ restore_from_backup()?;
let state = Arc::new(Mutex::new(None));
let watcher = DnsWatcher::start(state.clone())?;
@@ -54,8 +54,8 @@ impl StaticResolvConf {
let mut state = self.state.lock();
let new_state = match state.take() {
None => {
- let backup = read_config().chain_err(|| ErrorKind::BackupResolvConf)?;
- write_backup(&backup).chain_err(|| ErrorKind::BackupResolvConf)?;
+ let backup = read_config()?;
+ write_backup(&backup)?;
State {
backup,
@@ -111,11 +111,11 @@ struct DnsWatcher {
impl DnsWatcher {
fn start(state: Arc<Mutex<Option<State>>>) -> Result<Self> {
let (event_tx, event_rx) = mpsc::channel();
- let mut watcher = notify::raw_watcher(event_tx).chain_err(|| ErrorKind::WatchResolvConf)?;
+ let mut watcher = notify::raw_watcher(event_tx).map_err(Error::WatchResolvConf)?;
watcher
.watch(RESOLV_CONF_PATH, RecursiveMode::NonRecursive)
- .chain_err(|| ErrorKind::WatchResolvConf)?;
+ .map_err(Error::WatchResolvConf)?;
thread::spawn(move || Self::event_loop(event_rx, &state));
@@ -127,9 +127,12 @@ impl DnsWatcher {
let mut locked_state = state.lock();
if let Err(error) = Self::update(locked_state.as_mut()) {
- let chained_error = error
- .chain_err(|| "Failed to update DNS state after DNS settings have changed.");
- log::error!("{}", chained_error.display_chain());
+ log::error!(
+ "{}",
+ error.display_chain_with_msg(
+ "Failed to update DNS state after DNS settings changed"
+ )
+ );
}
}
}
@@ -153,7 +156,7 @@ impl DnsWatcher {
new_config.nameservers.append(&mut state.backup.nameservers);
state.backup = new_config;
- write_backup(&state.backup).chain_err(|| "Failed to update /etc/resolv.conf backup")
+ write_backup(&state.backup)
}
} else {
Ok(())
@@ -162,22 +165,21 @@ impl DnsWatcher {
}
fn read_config() -> Result<Config> {
- let contents =
- fs::read_to_string(RESOLV_CONF_PATH).chain_err(|| "Failed to read /etc/resolv.conf")?;
- let config =
- Config::parse(&contents).chain_err(|| "Failed to parse contents of /etc/resolv.conf")?;
+ let contents = fs::read_to_string(RESOLV_CONF_PATH)
+ .map_err(|e| Error::ReadResolvConf(RESOLV_CONF_PATH, e))?;
+ let config = Config::parse(&contents).map_err(|e| Error::ParseError(RESOLV_CONF_PATH, e))?;
Ok(config)
}
fn write_config(config: &Config) -> Result<()> {
fs::write(RESOLV_CONF_PATH, config.to_string().as_bytes())
- .chain_err(|| ErrorKind::WriteResolvConf)
+ .map_err(|e| Error::WriteResolvConf(RESOLV_CONF_PATH, e))
}
fn write_backup(backup: &Config) -> Result<()> {
fs::write(RESOLV_CONF_BACKUP_PATH, backup.to_string().as_bytes())
- .chain_err(|| "Failed to write to /etc/resolv.conf backup file")
+ .map_err(|e| Error::WriteResolvConf(RESOLV_CONF_BACKUP_PATH, e))
}
fn restore_from_backup() -> Result<()> {
@@ -185,20 +187,17 @@ fn restore_from_backup() -> Result<()> {
Ok(backup) => {
log::info!("Restoring DNS state from backup");
let config = Config::parse(&backup)
- .chain_err(|| "Backup of /etc/resolv.conf could not be parsed")?;
+ .map_err(|e| Error::ParseError(RESOLV_CONF_BACKUP_PATH, e))?;
write_config(&config)?;
fs::remove_file(RESOLV_CONF_BACKUP_PATH)
- .chain_err(|| "Failed to remove stale backup of /etc/resolv.conf")
+ .map_err(|e| Error::RemoveBackup(RESOLV_CONF_BACKUP_PATH, e))
}
Err(ref error) if error.kind() == io::ErrorKind::NotFound => {
log::debug!("No DNS state backup to restore");
Ok(())
}
- Err(error) => Err(Error::with_chain(
- error,
- "Failed to read /etc/resolv.conf backup",
- )),
+ Err(error) => Err(Error::ReadResolvConf(RESOLV_CONF_BACKUP_PATH, error)),
}
}