Allow custom data in errors to be displayed

This commit is contained in:
Matthias Beyer 2017-06-04 16:48:02 +02:00
parent e88c5011ab
commit 33390acf23
2 changed files with 23 additions and 1 deletions

View File

@ -118,7 +118,10 @@ macro_rules! generate_custom_error_types {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
try!(write!(fmt, "[{}]", self.err_type));
Ok(())
match self.custom_data {
Some(ref c) => write!(fmt, "{}", c),
None => Ok(()),
}
}
}
@ -215,6 +218,13 @@ macro_rules! generate_error_types {
) => {
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Copy)]
pub struct SomeNotExistingTypeWithATypeNameNoOneWillEverChoose {}
impl Display for SomeNotExistingTypeWithATypeNameNoOneWillEverChoose {
fn fmt(&self, _: &mut Formatter) -> Result<(), FmtError> {
Ok(())
}
}
generate_custom_error_types!($name, $kindname,
SomeNotExistingTypeWithATypeNameNoOneWillEverChoose,
$($kind => $string),*);
@ -241,6 +251,12 @@ mod test {
pub othr: i64,
}
impl Display for CustomData {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
Ok(())
}
}
generate_error_imports!();
#[allow(dead_code)]

View File

@ -23,6 +23,12 @@ use std::convert::From;
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Copy)]
pub struct CustomErrorData {}
impl Display for CustomErrorData {
fn fmt(&self, _: &mut Formatter) -> Result<(), FmtError> {
Ok(()) // Do nothing here, we don't need to print smth
}
}
generate_custom_error_types!(StoreError, StoreErrorKind, CustomErrorData,
ConfigurationError => "Store Configuration Error",
ConfigTypeError => "Store configuration type error",