Merge pull request #1472 from matthiasbeyer/libimagtodo/refactor-error-handing
Refactor error handling, use more error chaining
This commit is contained in:
commit
1e2e47c388
3 changed files with 16 additions and 17 deletions
|
@ -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")
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue