From b3cf96d88b96c3ec9a1d6417bb9da9ec5c969526 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 30 Apr 2018 17:20:36 +0200 Subject: [PATCH] Refactor error handling, use more error chaining --- lib/domain/libimagtodo/src/error.rs | 13 ++++++++----- lib/domain/libimagtodo/src/task.rs | 3 +-- lib/domain/libimagtodo/src/taskstore.rs | 17 +++++++---------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/domain/libimagtodo/src/error.rs b/lib/domain/libimagtodo/src/error.rs index e2402bb0..7498d387 100644 --- a/lib/domain/libimagtodo/src/error.rs +++ b/lib/domain/libimagtodo/src/error.rs @@ -22,17 +22,20 @@ error_chain! { TodoError, TodoErrorKind, ResultExt, Result; } + links { + StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind); + } + + foreign_links { + TomlQueryError(::toml_query::error::Error); + } + errors { ConversionError { description("Conversion Error") display("Conversion Error") } - StoreError { - description("Store Error") - display("Store Error") - } - StoreIdError { description("Store Id handling error") display("Store Id handling error") diff --git a/lib/domain/libimagtodo/src/task.rs b/lib/domain/libimagtodo/src/task.rs index 81fcc2c3..b16cec06 100644 --- a/lib/domain/libimagtodo/src/task.rs +++ b/lib/domain/libimagtodo/src/task.rs @@ -34,8 +34,7 @@ pub trait Task { impl Task for Entry { fn get_uuid(&self) -> Result { self.get_header() - .read_string("todo.uuid") - .chain_err(|| TEK::StoreError)? + .read_string("todo.uuid")? .ok_or(TE::from_kind(TEK::HeaderFieldMissing)) .and_then(|u| Uuid::parse_str(&u).chain_err(|| TEK::UuidParserError)) } diff --git a/lib/domain/libimagtodo/src/taskstore.rs b/lib/domain/libimagtodo/src/taskstore.rs index 103532ca..a85696e4 100644 --- a/lib/domain/libimagtodo/src/taskstore.rs +++ b/lib/domain/libimagtodo/src/taskstore.rs @@ -101,7 +101,7 @@ impl<'a> TaskStore<'a> for Store { ModuleEntryPath::new(format!("taskwarrior/{}", uuid)) .into_storeid() .and_then(|store_id| self.get(store_id)) - .chain_err(|| TEK::StoreError) + .map_err(TE::from) } /// Same as Task::get_from_import() but uses Store::retrieve() rather than Store::get(), to @@ -165,13 +165,13 @@ impl<'a> TaskStore<'a> for Store { ModuleEntryPath::new(format!("taskwarrior/{}", uuid)) .into_storeid() .and_then(|id| self.delete(id)) - .chain_err(|| TEK::StoreError) + .map_err(TE::from) } fn all_tasks(&self) -> Result { self.entries() .map(|i| TaskIdIterator::new(i.without_store())) - .chain_err(|| TEK::StoreError) + .map_err(TE::from) } fn new_from_twtask(&'a self, task: TTask) -> Result> { @@ -184,18 +184,15 @@ impl<'a> TaskStore<'a> for Store { .chain_err(|| TEK::StoreIdError) .and_then(|id| { self.retrieve(id) - .chain_err(|| TEK::StoreError) + .map_err(TE::from) .and_then(|mut fle| { { let hdr = fle.get_header_mut(); - if hdr.read("todo").chain_err(|| TEK::StoreError)?.is_none() { - hdr - .set("todo", Value::Table(BTreeMap::new())) - .chain_err(|| TEK::StoreError)?; + if hdr.read("todo")?.is_none() { + hdr.set("todo", Value::Table(BTreeMap::new()))?; } - hdr.set("todo.uuid", Value::String(format!("{}",uuid))) - .chain_err(|| TEK::StoreError)?; + hdr.set("todo.uuid", Value::String(format!("{}",uuid)))?; } // If none of the errors above have returned the function, everything is fine