Merge pull request #1339 from matthiasbeyer/libimagnotes/simplify-errorhandling

Simplify error handling by using linking feature of error-chain
This commit is contained in:
Matthias Beyer 2018-03-12 20:35:37 +01:00 committed by GitHub
commit f02276dabf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 23 deletions

View file

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

View file

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

View file

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