Simplify error handling by using linking feature of error-chain

This commit is contained in:
Matthias Beyer 2018-03-12 19:21:48 +01:00
parent 1bd91cfd0b
commit 5d9fd7ab10
3 changed files with 13 additions and 23 deletions

View file

@ -22,21 +22,15 @@ error_chain! {
NoteError, NoteErrorKind, ResultExt, Result;
}
links {
StoreError(::libimagstore::error::StoreError, ::libimagstore::error::StoreErrorKind);
}
foreign_links {
TomlQueryError(::toml_query::error::Error);
}
errors {
StoreWriteError {
description("Error writing store")
display("Error writing store")
}
StoreReadError {
description("Error reading store")
display("Error reading store")
}
HeaderTypeError {
description("Header type error")
display("Header type error")

View file

@ -25,9 +25,8 @@ use toml_query::read::TomlValueReadTypeExt;
use toml_query::set::TomlValueSetExt;
use error::Result;
use error::NoteErrorKind as NEK;
use error::NoteError as NE;
use error::ResultExt;
use error::NoteErrorKind as NEK;
pub trait Note {
fn set_name(&mut self, n: String) -> Result<()>;
@ -41,14 +40,13 @@ impl Note for Entry {
fn set_name(&mut self, n: String) -> Result<()> {
self.get_header_mut()
.set("note.name", Value::String(n))
.chain_err(|| NEK::StoreWriteError)
.map_err(NE::from)
.map(|_| ())
}
fn get_name(&self) -> Result<String> {
self.get_header()
.read_string("note.name")
.chain_err(|| NEK::StoreReadError)?
.read_string("note.name")?
.ok_or(NE::from_kind(NEK::HeaderTypeError))
}

View file

@ -27,8 +27,7 @@ use toml_query::insert::TomlValueInsertExt;
use module_path::ModuleEntryPath;
use error::Result;
use error::NoteErrorKind as NEK;
use error::ResultExt;
use error::NoteError as NE;
use iter::*;
pub trait NoteStore<'a> {
@ -49,8 +48,7 @@ impl<'a> NoteStore<'a> for Store {
let fle = {
let mut lockentry = ModuleEntryPath::new(name.clone())
.into_storeid()
.and_then(|id| self.create(id))
.chain_err(|| NEK::StoreWriteError)?;
.and_then(|id| self.create(id))?;
{
let entry = lockentry.deref_mut();
@ -68,27 +66,27 @@ impl<'a> NoteStore<'a> for Store {
ModuleEntryPath::new(name)
.into_storeid()
.and_then(|id| self.delete(id))
.chain_err(|| NEK::StoreWriteError)
.map_err(NE::from)
}
fn retrieve_note(&'a self, name: String) -> Result<FileLockEntry<'a>> {
ModuleEntryPath::new(name)
.into_storeid()
.and_then(|id| self.retrieve(id))
.chain_err(|| NEK::StoreWriteError)
.map_err(NE::from)
}
fn get_note(&'a self, name: String) -> Result<Option<FileLockEntry<'a>>> {
ModuleEntryPath::new(name)
.into_storeid()
.and_then(|id| self.get(id))
.chain_err(|| NEK::StoreWriteError)
.map_err(NE::from)
}
fn all_notes(&'a self) -> Result<NoteIterator> {
self.retrieve_for_module("notes")
.map(NoteIterator::new)
.chain_err(|| NEK::StoreReadError)
.map_err(NE::from)
}
}