Merge pull request #1472 from matthiasbeyer/libimagtodo/refactor-error-handing

Refactor error handling, use more error chaining
This commit is contained in:
Matthias Beyer 2018-04-30 18:40:42 +02:00 committed by GitHub
commit 1e2e47c388
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 17 deletions

View File

@ -22,17 +22,20 @@ error_chain! {
TodoError, TodoErrorKind, ResultExt, Result; TodoError, TodoErrorKind, ResultExt, Result;
} }
links {
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
}
foreign_links {
TomlQueryError(::toml_query::error::Error);
}
errors { errors {
ConversionError { ConversionError {
description("Conversion Error") description("Conversion Error")
display("Conversion Error") display("Conversion Error")
} }
StoreError {
description("Store Error")
display("Store Error")
}
StoreIdError { StoreIdError {
description("Store Id handling error") description("Store Id handling error")
display("Store Id handling error") display("Store Id handling error")

View File

@ -34,8 +34,7 @@ pub trait Task {
impl Task for Entry { impl Task for Entry {
fn get_uuid(&self) -> Result<Uuid> { fn get_uuid(&self) -> Result<Uuid> {
self.get_header() self.get_header()
.read_string("todo.uuid") .read_string("todo.uuid")?
.chain_err(|| TEK::StoreError)?
.ok_or(TE::from_kind(TEK::HeaderFieldMissing)) .ok_or(TE::from_kind(TEK::HeaderFieldMissing))
.and_then(|u| Uuid::parse_str(&u).chain_err(|| TEK::UuidParserError)) .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)) ModuleEntryPath::new(format!("taskwarrior/{}", uuid))
.into_storeid() .into_storeid()
.and_then(|store_id| self.get(store_id)) .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 /// 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)) ModuleEntryPath::new(format!("taskwarrior/{}", uuid))
.into_storeid() .into_storeid()
.and_then(|id| self.delete(id)) .and_then(|id| self.delete(id))
.chain_err(|| TEK::StoreError) .map_err(TE::from)
} }
fn all_tasks(&self) -> Result<TaskIdIterator> { fn all_tasks(&self) -> Result<TaskIdIterator> {
self.entries() self.entries()
.map(|i| TaskIdIterator::new(i.without_store())) .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>> { 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) .chain_err(|| TEK::StoreIdError)
.and_then(|id| { .and_then(|id| {
self.retrieve(id) self.retrieve(id)
.chain_err(|| TEK::StoreError) .map_err(TE::from)
.and_then(|mut fle| { .and_then(|mut fle| {
{ {
let hdr = fle.get_header_mut(); let hdr = fle.get_header_mut();
if hdr.read("todo").chain_err(|| TEK::StoreError)?.is_none() { if hdr.read("todo")?.is_none() {
hdr hdr.set("todo", Value::Table(BTreeMap::new()))?;
.set("todo", Value::Table(BTreeMap::new()))
.chain_err(|| TEK::StoreError)?;
} }
hdr.set("todo.uuid", Value::String(format!("{}",uuid))) hdr.set("todo.uuid", Value::String(format!("{}",uuid)))?;
.chain_err(|| TEK::StoreError)?;
} }
// If none of the errors above have returned the function, everything is fine // If none of the errors above have returned the function, everything is fine