From 31dc0eebc28b0ef2d5f44107e79ead846591e59f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 3 Sep 2017 16:09:55 +0200 Subject: [PATCH] libimagcounter: Rewrite error handling --- lib/domain/libimagcounter/src/counter.rs | 34 ++++++++++++------------ lib/domain/libimagcounter/src/error.rs | 9 ++++--- lib/domain/libimagcounter/src/lib.rs | 2 +- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/domain/libimagcounter/src/counter.rs b/lib/domain/libimagcounter/src/counter.rs index b34219a0..e23a18df 100644 --- a/lib/domain/libimagcounter/src/counter.rs +++ b/lib/domain/libimagcounter/src/counter.rs @@ -38,7 +38,7 @@ use module_path::ModuleEntryPath; use result::Result; use error::CounterError as CE; use error::CounterErrorKind as CEK; -use error::error::MapErrInto; +use error::ResultExt; pub type CounterName = String; @@ -71,8 +71,8 @@ impl<'a> Counter<'a> { let fle = { let id = try!(ModuleEntryPath::new(name.clone()) .into_storeid() - .map_err_into(CEK::StoreWriteError)); - let mut lockentry = try!(store.create(id).map_err_into(CEK::StoreWriteError)); + .chain_err(|| CEK::StoreWriteError)); + let mut lockentry = try!(store.create(id).chain_err(|| CEK::StoreWriteError)); { let entry = lockentry.deref_mut(); @@ -116,26 +116,26 @@ impl<'a> Counter<'a> { pub fn inc(&mut self) -> Result<()> { let header = self.fle.deref_mut().get_header_mut(); let query = String::from("counter.value"); - match try!(header.read(&query).map_err_into(CEK::StoreReadError)) { + match try!(header.read(&query).chain_err(|| CEK::StoreReadError)) { Some(&Value::Integer(i)) => { header.set(&query, Value::Integer(i + 1)) - .map_err_into(CEK::StoreWriteError) + .chain_err(|| CEK::StoreWriteError) .map(|_| ()) }, - _ => Err(CE::new(CEK::StoreReadError, None)), + _ => Err(CE::from_kind(CEK::StoreReadError)), } } pub fn dec(&mut self) -> Result<()> { let header = self.fle.deref_mut().get_header_mut(); let query = String::from("counter.value"); - match try!(header.read(&query).map_err_into(CEK::StoreReadError)) { + match try!(header.read(&query).chain_err(|| CEK::StoreReadError)) { Some(&Value::Integer(i)) => { header.set(&query, Value::Integer(i - 1)) - .map_err_into(CEK::StoreWriteError) + .chain_err(|| CEK::StoreWriteError) .map(|_| ()) }, - _ => Err(CE::new(CEK::StoreReadError, None)), + _ => Err(CE::from_kind(CEK::StoreReadError)), } } @@ -148,7 +148,7 @@ impl<'a> Counter<'a> { .deref_mut() .get_header_mut() .set(&String::from("counter.value"), Value::Integer(v)) - .map_err_into(CEK::StoreWriteError) + .chain_err(|| CEK::StoreWriteError) .map(|_| ()) } @@ -187,7 +187,7 @@ impl<'a> Counter<'a> { self.fle .get_header() .read(&String::from(name)) - .map_err_into(CEK::StoreWriteError) + .chain_err(|| CEK::StoreWriteError) .and_then(f) } @@ -195,7 +195,7 @@ impl<'a> Counter<'a> { debug!("Loading counter: '{}'", name); let id = try!(ModuleEntryPath::new(name) .into_storeid() - .map_err_into(CEK::StoreWriteError)); + .chain_err(|| CEK::StoreWriteError)); Counter::from_storeid(store, id) } @@ -203,14 +203,14 @@ impl<'a> Counter<'a> { debug!("Deleting counter: '{}'", name); let id = try!(ModuleEntryPath::new(name) .into_storeid() - .map_err_into(CEK::StoreWriteError)); - store.delete(id).map_err_into(CEK::StoreWriteError) + .chain_err(|| CEK::StoreWriteError)); + store.delete(id).chain_err(|| CEK::StoreWriteError) } pub fn all_counters(store: &Store) -> Result { store.retrieve_for_module("counter") .map(|iter| CounterIterator::new(store, iter)) - .map_err_into(CEK::StoreReadError) + .chain_err(|| CEK::StoreReadError) } } @@ -224,11 +224,11 @@ impl<'a> FromStoreId for Counter<'a> { fn from_storeid(store: &Store, id: StoreId) -> Result { debug!("Loading counter from storeid: '{:?}'", id); match store.retrieve(id) { - Err(e) => Err(CE::new(CEK::StoreReadError, Some(Box::new(e)))), + Err(e) => Err(e).chain_err(|| CEK::StoreReadError), Ok(c) => { let mut counter = Counter { fle: c, unit: None }; counter.read_unit() - .map_err_into(CEK::StoreReadError) + .chain_err(|| CEK::StoreReadError) .and_then(|u| { counter.unit = u; Ok(counter) diff --git a/lib/domain/libimagcounter/src/error.rs b/lib/domain/libimagcounter/src/error.rs index 7bf42d16..bca5a0b1 100644 --- a/lib/domain/libimagcounter/src/error.rs +++ b/lib/domain/libimagcounter/src/error.rs @@ -17,6 +17,10 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use std::error::Error; + +use libimagerror::into::IntoError; + error_chain! { types { CounterError, CounterErrorKind, ResultExt, Result; @@ -51,9 +55,6 @@ error_chain! { } } -pub use self::error::CounterError; -pub use self::error::CounterErrorKind; - impl IntoError for CounterErrorKind { type Target = CounterError; @@ -61,7 +62,7 @@ impl IntoError for CounterErrorKind { CounterError::from_kind(self) } - fn into_error_with_cause(self, cause: Box) -> Self::Target { + fn into_error_with_cause(self, _: Box) -> Self::Target { CounterError::from_kind(self) } } diff --git a/lib/domain/libimagcounter/src/lib.rs b/lib/domain/libimagcounter/src/lib.rs index a023437e..6aa28b71 100644 --- a/lib/domain/libimagcounter/src/lib.rs +++ b/lib/domain/libimagcounter/src/lib.rs @@ -41,7 +41,7 @@ extern crate toml_query; #[macro_use] extern crate error_chain; #[macro_use] extern crate libimagstore; -#[macro_use] extern crate libimagerror; +extern crate libimagerror; module_entry_path_mod!("counter");