libimaginteraction: Rewrite error handling

This commit is contained in:
Matthias Beyer 2017-09-03 14:43:02 +02:00
parent b6909a2c86
commit ac34b4ee66
4 changed files with 14 additions and 14 deletions

View file

@ -24,8 +24,8 @@ use std::io::BufRead;
use std::io::BufReader; use std::io::BufReader;
use std::result::Result as RResult; use std::result::Result as RResult;
use error::InteractionError;
use error::InteractionErrorKind; use error::InteractionErrorKind;
use error::ResultExt;
use result::Result; use result::Result;
use regex::Regex; use regex::Regex;
@ -163,7 +163,7 @@ fn ask_string_<R: BufRead>(s: &str,
pub fn ask_select_from_list(list: &[&str]) -> Result<String> { pub fn ask_select_from_list(list: &[&str]) -> Result<String> {
pick_from_list(default_menu_cmd().as_mut(), list, "Selection: ") pick_from_list(default_menu_cmd().as_mut(), list, "Selection: ")
.map_err(|e| InteractionError::new(InteractionErrorKind::Unknown, Some(Box::new(e)))) .chain_err(|| InteractionErrorKind::Unknown)
} }
/// Helper function to print a imag question string. The `question` argument may not contain a /// Helper function to print a imag question string. The `question` argument may not contain a

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

View file

@ -44,7 +44,7 @@ extern crate toml;
#[macro_use] extern crate error_chain; #[macro_use] extern crate error_chain;
extern crate libimagstore; extern crate libimagstore;
#[macro_use] extern crate libimagerror; extern crate libimagerror;
pub mod ask; pub mod ask;
pub mod error; pub mod error;

View file

@ -25,8 +25,8 @@ use libimagstore::storeid::StoreId;
use libimagerror::into::IntoError; use libimagerror::into::IntoError;
use result::Result; use result::Result;
use error::MapErrInto;
use error::InteractionErrorKind as IEK; use error::InteractionErrorKind as IEK;
use error::ResultExt;
pub fn id_argument<'a, 'b>() -> Arg<'a, 'b> { pub fn id_argument<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name(id_argument_name()) Arg::with_name(id_argument_name())
@ -53,13 +53,13 @@ pub fn get_id(matches: &ArgMatches) -> Result<Vec<StoreId>> {
matches matches
.values_of(id_argument_name()) .values_of(id_argument_name())
.ok_or(IEK::IdMissingError.into_error()) .ok_or(IEK::IdMissingError.into_error())
.map_err_into(IEK::CLIError) .chain_err(|| IEK::CLIError)
.and_then(|vals| { .and_then(|vals| {
vals.into_iter() vals.into_iter()
.fold(Ok(vec![]), |acc, elem| { .fold(Ok(vec![]), |acc, elem| {
acc.and_then(|mut v| { acc.and_then(|mut v| {
let elem = StoreId::new_baseless(PathBuf::from(String::from(elem))); let elem = StoreId::new_baseless(PathBuf::from(String::from(elem)));
let elem = try!(elem.map_err_into(IEK::StoreIdParsingError)); let elem = try!(elem.chain_err(|| IEK::StoreIdParsingError));
v.push(elem); v.push(elem);
Ok(v) Ok(v)
}) })
@ -70,12 +70,12 @@ pub fn get_id(matches: &ArgMatches) -> Result<Vec<StoreId>> {
pub fn get_or_select_id(matches: &ArgMatches, store_path: &PathBuf) -> Result<Vec<StoreId>> { pub fn get_or_select_id(matches: &ArgMatches, store_path: &PathBuf) -> Result<Vec<StoreId>> {
use interactor::{pick_file, default_menu_cmd}; use interactor::{pick_file, default_menu_cmd};
match get_id(matches).map_err_into(IEK::IdSelectingError) { match get_id(matches).chain_err(|| IEK::IdSelectingError) {
Ok(v) => Ok(v), Ok(v) => Ok(v),
Err(_) => { Err(_) => {
let path = store_path.clone(); let path = store_path.clone();
let p = try!(pick_file(default_menu_cmd, path).map_err_into(IEK::IdSelectingError)); let p = try!(pick_file(default_menu_cmd, path).chain_err(|| IEK::IdSelectingError));
let id = try!(StoreId::new_baseless(p).map_err_into(IEK::StoreIdParsingError)); let id = try!(StoreId::new_baseless(p).chain_err(|| IEK::StoreIdParsingError));
Ok(vec![id]) Ok(vec![id])
}, },
} }