libimagentrylist: Rewrite error handling

This commit is contained in:
Matthias Beyer 2017-09-03 15:10:11 +02:00
parent 4b4b0b0804
commit 9aa5d7439d
7 changed files with 19 additions and 29 deletions

View file

@ -20,12 +20,13 @@
use clap::{Arg, ArgMatches, App, SubCommand}; use clap::{Arg, ArgMatches, App, SubCommand};
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagerror::into::IntoError;
use result::Result; use result::Result;
use listers::line::LineLister; use listers::line::LineLister;
use listers::path::PathLister; use listers::path::PathLister;
use lister::Lister; use lister::Lister;
use error::{ListError, ListErrorKind}; use error::ListErrorKind;
pub fn build_list_cli_component<'a, 'b>() -> App<'a, 'b> { pub fn build_list_cli_component<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name(list_subcommand_name()) SubCommand::with_name(list_subcommand_name())
@ -96,6 +97,6 @@ pub fn list_entries_with_lister<'a, I>(m: &ArgMatches, entries: I) -> Result<()>
Ok(()) Ok(())
} else { } else {
Err(ListError::new(ListErrorKind::CLIError, None)) Err(ListErrorKind::CLIError.into_error())
} }
} }

View file

@ -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 {
ListError, ListErrorKind, ResultExt, Result; ListError, ListErrorKind, ResultExt, Result;
@ -51,10 +55,6 @@ error_chain! {
} }
} }
pub use self::error::ListError;
pub use self::error::ListErrorKind;
pub use self::error::MapErrInto;
impl IntoError for ListErrorKind { impl IntoError for ListErrorKind {
type Target = ListError; type Target = ListError;
@ -62,7 +62,7 @@ impl IntoError for ListErrorKind {
ListError::from_kind(self) ListError::from_kind(self)
} }
fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target { fn into_error_with_cause(self, _: Box<Error>) -> Self::Target {
ListError::from_kind(self) ListError::from_kind(self)
} }
} }

View file

@ -42,7 +42,7 @@ extern crate prettytable;
extern crate libimagstore; extern crate libimagstore;
extern crate libimagutil; extern crate libimagutil;
#[macro_use] extern crate libimagerror; extern crate libimagerror;
pub mod cli; pub mod cli;
pub mod error; pub mod error;

View file

@ -22,6 +22,7 @@ use std::io::Write;
use lister::Lister; use lister::Lister;
use result::Result; use result::Result;
use error::ResultExt;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagstore::store::Entry; use libimagstore::store::Entry;
@ -43,7 +44,6 @@ impl<T: Fn(&Entry) -> String> CoreLister<T> {
impl<T: Fn(&Entry) -> String> Lister for CoreLister<T> { impl<T: Fn(&Entry) -> String> Lister for CoreLister<T> {
fn list<'b, I: Iterator<Item = FileLockEntry<'b>>>(&self, entries: I) -> Result<()> { fn list<'b, I: Iterator<Item = FileLockEntry<'b>>>(&self, entries: I) -> Result<()> {
use error::ListError as LE;
use error::ListErrorKind as LEK; use error::ListErrorKind as LEK;
debug!("Called list()"); debug!("Called list()");
@ -53,7 +53,7 @@ impl<T: Fn(&Entry) -> String> Lister for CoreLister<T> {
let r = accu.and_then(|_| { let r = accu.and_then(|_| {
debug!("Listing Entry: {:?}", entry); debug!("Listing Entry: {:?}", entry);
write!(stdout(), "{:?}\n", (self.lister)(&entry)) write!(stdout(), "{:?}\n", (self.lister)(&entry))
.map_err(|e| LE::new(LEK::FormatError, Some(Box::new(e)))) .chain_err(|| LEK::FormatError)
}); });
(r, i + 1) (r, i + 1)
}); });

View file

@ -22,6 +22,7 @@ use std::io::Write;
use lister::Lister; use lister::Lister;
use result::Result; use result::Result;
use error::ResultExt;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagutil::iter::FoldResult; use libimagutil::iter::FoldResult;
@ -43,12 +44,11 @@ impl<'a> LineLister<'a> {
impl<'a> Lister for LineLister<'a> { impl<'a> Lister for LineLister<'a> {
fn list<'b, I: Iterator<Item = FileLockEntry<'b>>>(&self, entries: I) -> Result<()> { fn list<'b, I: Iterator<Item = FileLockEntry<'b>>>(&self, entries: I) -> Result<()> {
use error::ListError as LE;
use error::ListErrorKind as LEK; use error::ListErrorKind as LEK;
entries.fold_result(|entry| { entries.fold_result(|entry| {
let s = entry.get_location().to_str().unwrap_or(String::from(self.unknown_output)); let s = entry.get_location().to_str().unwrap_or(String::from(self.unknown_output));
write!(stdout(), "{:?}\n", s).map_err(|e| LE::new(LEK::FormatError, Some(Box::new(e)))) write!(stdout(), "{:?}\n", s).chain_err(| | LEK::FormatError)
}) })
} }

View file

@ -22,7 +22,7 @@ use std::io::Write;
use lister::Lister; use lister::Lister;
use result::Result; use result::Result;
use error::MapErrInto; use error::ResultExt;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagutil::iter::FoldResult; use libimagutil::iter::FoldResult;
@ -44,30 +44,19 @@ impl PathLister {
impl Lister for PathLister { impl Lister for PathLister {
fn list<'a, I: Iterator<Item = FileLockEntry<'a>>>(&self, entries: I) -> Result<()> { fn list<'a, I: Iterator<Item = FileLockEntry<'a>>>(&self, entries: I) -> Result<()> {
use error::ListError as LE;
use error::ListErrorKind as LEK; use error::ListErrorKind as LEK;
entries.fold_result(|entry| { entries.fold_result(|entry| {
Ok(entry.get_location().clone()) Ok(entry.get_location().clone())
.and_then(|pb| pb.into_pathbuf().map_err_into(LEK::FormatError)) .and_then(|pb| pb.into_pathbuf().chain_err(|| LEK::FormatError))
.and_then(|pb| { .and_then(|pb| {
if self.absolute { if self.absolute {
pb.canonicalize().map_err(|e| LE::new(LEK::FormatError, Some(Box::new(e)))) pb.canonicalize().chain_err(|| LEK::FormatError)
} else { } else {
Ok(pb.into()) Ok(pb.into())
} }
}) })
.and_then(|pb| { .and_then(|pb| write!(stdout(), "{:?}\n", pb).chain_err(|| LEK::FormatError))
write!(stdout(), "{:?}\n", pb)
.map_err(|e| LE::new(LEK::FormatError, Some(Box::new(e))))
})
.map_err(|e| {
if e.err_type() == LEK::FormatError {
e
} else {
LE::new(LEK::FormatError, Some(Box::new(e)))
}
})
}) })
} }

View file

@ -21,7 +21,7 @@ use std::io::stdout;
use lister::Lister; use lister::Lister;
use result::Result; use result::Result;
use error::MapErrInto; use error::ResultExt;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagerror::into::IntoError; use libimagerror::into::IntoError;
@ -103,7 +103,7 @@ impl<F: Fn(&FileLockEntry) -> Vec<String>> Lister for TableLister<F> {
}) })
.and_then(|tbl| { .and_then(|tbl| {
let mut io = stdout(); let mut io = stdout();
tbl.print(&mut io).map_err_into(LEK::IOError) tbl.print(&mut io).chain_err(|| LEK::IOError)
}) })
} }