From 0b068df84e3063b24ece69d50d39c30c25e85a40 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 3 Sep 2017 15:34:58 +0200 Subject: [PATCH] libimagnotes: Rewrite error handling --- lib/domain/libimagnotes/src/error.rs | 10 +++---- lib/domain/libimagnotes/src/lib.rs | 2 +- lib/domain/libimagnotes/src/note.rs | 41 ++++++++++++---------------- 3 files changed, 24 insertions(+), 29 deletions(-) diff --git a/lib/domain/libimagnotes/src/error.rs b/lib/domain/libimagnotes/src/error.rs index 8b7e6186..df84ef85 100644 --- a/lib/domain/libimagnotes/src/error.rs +++ b/lib/domain/libimagnotes/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 { NoteError, NoteErrorKind, ResultExt, Result; @@ -46,10 +50,6 @@ error_chain! { } } -pub use self::error::NoteError; -pub use self::error::NoteErrorKind; -pub use self::error::MapErrInto; - impl IntoError for NoteErrorKind { type Target = NoteError; @@ -57,7 +57,7 @@ impl IntoError for NoteErrorKind { NoteError::from_kind(self) } - fn into_error_with_cause(self, cause: Box) -> Self::Target { + fn into_error_with_cause(self, _: Box) -> Self::Target { NoteError::from_kind(self) } } diff --git a/lib/domain/libimagnotes/src/lib.rs b/lib/domain/libimagnotes/src/lib.rs index fd646913..54315538 100644 --- a/lib/domain/libimagnotes/src/lib.rs +++ b/lib/domain/libimagnotes/src/lib.rs @@ -42,7 +42,7 @@ extern crate toml_query; extern crate libimagrt; #[macro_use] extern crate libimagstore; -#[macro_use] extern crate libimagerror; +extern crate libimagerror; extern crate libimagentryedit; module_entry_path_mod!("notes"); diff --git a/lib/domain/libimagnotes/src/note.rs b/lib/domain/libimagnotes/src/note.rs index feaa8818..9030ce9d 100644 --- a/lib/domain/libimagnotes/src/note.rs +++ b/lib/domain/libimagnotes/src/note.rs @@ -25,6 +25,7 @@ use toml::Value; use libimagrt::runtime::Runtime; use libimagentryedit::edit::Edit; use libimagentryedit::result::Result as EditResult; +use libimagerror::into::IntoError; use libimagstore::storeid::IntoStoreId; use libimagstore::storeid::StoreId; use libimagstore::storeid::StoreIdIterator; @@ -36,9 +37,8 @@ use toml_query::set::TomlValueSetExt; use module_path::ModuleEntryPath; use result::Result; -use error::NoteError as NE; use error::NoteErrorKind as NEK; -use error::MapErrInto; +use error::ResultExt; #[derive(Debug)] pub struct Note<'a> { @@ -55,24 +55,20 @@ impl<'a> Note<'a> { let mut lockentry = try!(ModuleEntryPath::new(name.clone()) .into_storeid() .and_then(|id| store.create(id)) - .map_err_into(NEK::StoreWriteError)); + .chain_err(|| NEK::StoreWriteError)); { let entry = lockentry.deref_mut(); { let header = entry.get_header_mut(); - let setres = header.set("note", Value::Table(BTreeMap::new())); - if setres.is_err() { - let kind = NEK::StoreWriteError; - return Err(NE::new(kind, Some(Box::new(setres.unwrap_err())))); - } + let _ = header + .set("note", Value::Table(BTreeMap::new())) + .chain_err(|| NEK::StoreWriteError); - let setres = header.set("note.name", Value::String(name)); - if setres.is_err() { - let kind = NEK::StoreWriteError; - return Err(NE::new(kind, Some(Box::new(setres.unwrap_err())))); - } + let _ = header + .set("note.name", Value::String(name)) + .chain_err(|| NEK::StoreWriteError); } *entry.get_content_mut() = text; @@ -88,7 +84,7 @@ impl<'a> Note<'a> { self.entry .get_header_mut() .set("note.name", Value::String(n)) - .map_err(|e| NE::new(NEK::StoreWriteError, Some(Box::new(e)))) + .chain_err(|| NEK::StoreWriteError) .map(|_| ()) } @@ -96,11 +92,10 @@ impl<'a> Note<'a> { let header = self.entry.get_header(); match header.read("note.name") { Ok(Some(&Value::String(ref s))) => Ok(s.clone()), - Ok(_) => { - let e = NE::new(NEK::HeaderTypeError, None); - Err(NE::new(NEK::StoreReadError, Some(Box::new(e)))) + Ok(_) => { + Err(NEK::HeaderTypeError.into_error()).chain_err(|| NEK::StoreReadError) }, - Err(e) => Err(NE::new(NEK::StoreReadError, Some(Box::new(e)))) + Err(e) => Err(e).chain_err(|| NEK::StoreReadError) } } @@ -116,14 +111,14 @@ impl<'a> Note<'a> { ModuleEntryPath::new(name) .into_storeid() .and_then(|id| store.delete(id)) - .map_err_into(NEK::StoreWriteError) + .chain_err(|| NEK::StoreWriteError) } pub fn retrieve(store: &Store, name: String) -> Result { ModuleEntryPath::new(name) .into_storeid() .and_then(|id| store.retrieve(id)) - .map_err_into(NEK::StoreWriteError) + .chain_err(|| NEK::StoreWriteError) .map(|entry| Note { entry: entry }) } @@ -131,14 +126,14 @@ impl<'a> Note<'a> { ModuleEntryPath::new(name) .into_storeid() .and_then(|id| store.get(id)) - .map_err_into(NEK::StoreWriteError) + .chain_err(|| NEK::StoreWriteError) .map(|o| o.map(|entry| Note { entry: entry })) } pub fn all_notes(store: &Store) -> Result { store.retrieve_for_module("notes") .map(|iter| NoteIterator::new(store, iter)) - .map_err(|e| NE::new(NEK::StoreReadError, Some(Box::new(e)))) + .chain_err(|| NEK::StoreReadError) } } @@ -160,7 +155,7 @@ impl<'a> FromStoreId for Note<'a> { fn from_storeid(store: &Store, id: StoreId) -> Result { debug!("Loading note from storeid: '{:?}'", id); match store.retrieve(id) { - Err(e) => Err(NE::new(NEK::StoreReadError, Some(Box::new(e)))), + Err(e) => Err(e).chain_err(|| NEK::StoreReadError), Ok(entry) => Ok(Note { entry: entry }), } }