Refactor error handling, use more error chaining

This commit is contained in:
Matthias Beyer 2018-04-30 17:20:36 +02:00
parent 76966bcd6c
commit b3cf96d88b
3 changed files with 16 additions and 17 deletions

View file

@ -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")

View file

@ -34,8 +34,7 @@ pub trait Task {
impl Task for Entry {
fn get_uuid(&self) -> Result<Uuid> {
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))
}

View file

@ -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<TaskIdIterator> {
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<FileLockEntry<'a>> {
@ -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