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