libimagnotes: Rewrite error handling
This commit is contained in:
parent
22be1627b7
commit
0b068df84e
3 changed files with 24 additions and 29 deletions
|
@ -17,6 +17,10 @@
|
|||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
//
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
use libimagerror::into::IntoError;
|
||||
|
||||
error_chain! {
|
||||
types {
|
||||
NoteError, NoteErrorKind, ResultExt, Result;
|
||||
|
@ -46,10 +50,6 @@ error_chain! {
|
|||
}
|
||||
}
|
||||
|
||||
pub use self::error::NoteError;
|
||||
pub use self::error::NoteErrorKind;
|
||||
pub use self::error::MapErrInto;
|
||||
|
||||
impl IntoError for NoteErrorKind {
|
||||
type Target = NoteError;
|
||||
|
||||
|
@ -57,7 +57,7 @@ impl IntoError for NoteErrorKind {
|
|||
NoteError::from_kind(self)
|
||||
}
|
||||
|
||||
fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target {
|
||||
fn into_error_with_cause(self, _: Box<Error>) -> Self::Target {
|
||||
NoteError::from_kind(self)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ extern crate toml_query;
|
|||
|
||||
extern crate libimagrt;
|
||||
#[macro_use] extern crate libimagstore;
|
||||
#[macro_use] extern crate libimagerror;
|
||||
extern crate libimagerror;
|
||||
extern crate libimagentryedit;
|
||||
|
||||
module_entry_path_mod!("notes");
|
||||
|
|
|
@ -25,6 +25,7 @@ use toml::Value;
|
|||
use libimagrt::runtime::Runtime;
|
||||
use libimagentryedit::edit::Edit;
|
||||
use libimagentryedit::result::Result as EditResult;
|
||||
use libimagerror::into::IntoError;
|
||||
use libimagstore::storeid::IntoStoreId;
|
||||
use libimagstore::storeid::StoreId;
|
||||
use libimagstore::storeid::StoreIdIterator;
|
||||
|
@ -36,9 +37,8 @@ use toml_query::set::TomlValueSetExt;
|
|||
|
||||
use module_path::ModuleEntryPath;
|
||||
use result::Result;
|
||||
use error::NoteError as NE;
|
||||
use error::NoteErrorKind as NEK;
|
||||
use error::MapErrInto;
|
||||
use error::ResultExt;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Note<'a> {
|
||||
|
@ -55,24 +55,20 @@ impl<'a> Note<'a> {
|
|||
let mut lockentry = try!(ModuleEntryPath::new(name.clone())
|
||||
.into_storeid()
|
||||
.and_then(|id| store.create(id))
|
||||
.map_err_into(NEK::StoreWriteError));
|
||||
.chain_err(|| NEK::StoreWriteError));
|
||||
|
||||
{
|
||||
let entry = lockentry.deref_mut();
|
||||
|
||||
{
|
||||
let header = entry.get_header_mut();
|
||||
let setres = header.set("note", Value::Table(BTreeMap::new()));
|
||||
if setres.is_err() {
|
||||
let kind = NEK::StoreWriteError;
|
||||
return Err(NE::new(kind, Some(Box::new(setres.unwrap_err()))));
|
||||
}
|
||||
let _ = header
|
||||
.set("note", Value::Table(BTreeMap::new()))
|
||||
.chain_err(|| NEK::StoreWriteError);
|
||||
|
||||
let setres = header.set("note.name", Value::String(name));
|
||||
if setres.is_err() {
|
||||
let kind = NEK::StoreWriteError;
|
||||
return Err(NE::new(kind, Some(Box::new(setres.unwrap_err()))));
|
||||
}
|
||||
let _ = header
|
||||
.set("note.name", Value::String(name))
|
||||
.chain_err(|| NEK::StoreWriteError);
|
||||
}
|
||||
|
||||
*entry.get_content_mut() = text;
|
||||
|
@ -88,7 +84,7 @@ impl<'a> Note<'a> {
|
|||
self.entry
|
||||
.get_header_mut()
|
||||
.set("note.name", Value::String(n))
|
||||
.map_err(|e| NE::new(NEK::StoreWriteError, Some(Box::new(e))))
|
||||
.chain_err(|| NEK::StoreWriteError)
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
|
@ -97,10 +93,9 @@ impl<'a> Note<'a> {
|
|||
match header.read("note.name") {
|
||||
Ok(Some(&Value::String(ref s))) => Ok(s.clone()),
|
||||
Ok(_) => {
|
||||
let e = NE::new(NEK::HeaderTypeError, None);
|
||||
Err(NE::new(NEK::StoreReadError, Some(Box::new(e))))
|
||||
Err(NEK::HeaderTypeError.into_error()).chain_err(|| NEK::StoreReadError)
|
||||
},
|
||||
Err(e) => Err(NE::new(NEK::StoreReadError, Some(Box::new(e))))
|
||||
Err(e) => Err(e).chain_err(|| NEK::StoreReadError)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,14 +111,14 @@ impl<'a> Note<'a> {
|
|||
ModuleEntryPath::new(name)
|
||||
.into_storeid()
|
||||
.and_then(|id| store.delete(id))
|
||||
.map_err_into(NEK::StoreWriteError)
|
||||
.chain_err(|| NEK::StoreWriteError)
|
||||
}
|
||||
|
||||
pub fn retrieve(store: &Store, name: String) -> Result<Note> {
|
||||
ModuleEntryPath::new(name)
|
||||
.into_storeid()
|
||||
.and_then(|id| store.retrieve(id))
|
||||
.map_err_into(NEK::StoreWriteError)
|
||||
.chain_err(|| NEK::StoreWriteError)
|
||||
.map(|entry| Note { entry: entry })
|
||||
}
|
||||
|
||||
|
@ -131,14 +126,14 @@ impl<'a> Note<'a> {
|
|||
ModuleEntryPath::new(name)
|
||||
.into_storeid()
|
||||
.and_then(|id| store.get(id))
|
||||
.map_err_into(NEK::StoreWriteError)
|
||||
.chain_err(|| NEK::StoreWriteError)
|
||||
.map(|o| o.map(|entry| Note { entry: entry }))
|
||||
}
|
||||
|
||||
pub fn all_notes(store: &Store) -> Result<NoteIterator> {
|
||||
store.retrieve_for_module("notes")
|
||||
.map(|iter| NoteIterator::new(store, iter))
|
||||
.map_err(|e| NE::new(NEK::StoreReadError, Some(Box::new(e))))
|
||||
.chain_err(|| NEK::StoreReadError)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -160,7 +155,7 @@ impl<'a> FromStoreId for Note<'a> {
|
|||
fn from_storeid(store: &Store, id: StoreId) -> Result<Note> {
|
||||
debug!("Loading note from storeid: '{:?}'", id);
|
||||
match store.retrieve(id) {
|
||||
Err(e) => Err(NE::new(NEK::StoreReadError, Some(Box::new(e)))),
|
||||
Err(e) => Err(e).chain_err(|| NEK::StoreReadError),
|
||||
Ok(entry) => Ok(Note { entry: entry }),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue