mirror of
https://github.com/haraldh/chainerror.git
synced 2025-01-31 00:56:41 +01:00
ii
This commit is contained in:
parent
8017b6cdfd
commit
e4c34740be
101
src/lib.rs
101
src/lib.rs
|
@ -45,6 +45,26 @@ macro_rules! chain_error_fn {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! into_boxed_chain_error_fn {
|
||||||
|
( $v:expr $(, $more:expr)* ) => {
|
||||||
|
|e| Box::<Error>::from(e).into_chain_error(line!(), file!(), Some(format!($v, $( $more , )* )))
|
||||||
|
};
|
||||||
|
( ) => {
|
||||||
|
|e| Box::<Error>::from(e).into_chain_error(line!(), file!(), None)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! chain {
|
||||||
|
( $v:expr $(, $more:expr)* ) => {
|
||||||
|
|e| Box::<Error>::from(e).into_chain_error(line!(), file!(), Some(format!($v, $( $more , )* )))
|
||||||
|
};
|
||||||
|
( ) => {
|
||||||
|
|e| Box::<Error>::from(e).into_chain_error(line!(), file!(), None)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! into_chain_error_fn {
|
macro_rules! into_chain_error_fn {
|
||||||
( $v:expr $(, $more:expr)* ) => {
|
( $v:expr $(, $more:expr)* ) => {
|
||||||
|
@ -167,13 +187,7 @@ macro_rules! derive_chain_error {
|
||||||
|
|
||||||
impl ::std::fmt::Debug for $e {
|
impl ::std::fmt::Debug for $e {
|
||||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||||
writeln!(
|
writeln!(f, "{}:{}: {}", self.filename, self.line, self.description())?;
|
||||||
f,
|
|
||||||
"\n{}:{}: {}",
|
|
||||||
self.filename,
|
|
||||||
self.line,
|
|
||||||
self.description()
|
|
||||||
)?;
|
|
||||||
if let Some(e) = self.source() {
|
if let Some(e) = self.source() {
|
||||||
writeln!(f, "\nCaused by:")?;
|
writeln!(f, "\nCaused by:")?;
|
||||||
::std::fmt::Debug::fmt(&e, f)?;
|
::std::fmt::Debug::fmt(&e, f)?;
|
||||||
|
@ -200,6 +214,22 @@ mod tests {
|
||||||
derive_chain_error!(MyError);
|
derive_chain_error!(MyError);
|
||||||
derive_chain_error!(MyMainError);
|
derive_chain_error!(MyMainError);
|
||||||
|
|
||||||
|
impl ChainErrorFrom<Box<Error>> for MyMainError {
|
||||||
|
fn chain_error_from(
|
||||||
|
e: Box<Error>,
|
||||||
|
line: u32,
|
||||||
|
filename: &'static str,
|
||||||
|
description: Option<String>,
|
||||||
|
) -> Self {
|
||||||
|
MyMainError {
|
||||||
|
line,
|
||||||
|
filename,
|
||||||
|
description,
|
||||||
|
error_cause: Some(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn throw_error() -> Result<(), MyError> {
|
fn throw_error() -> Result<(), MyError> {
|
||||||
let directory = String::from("ldfhgdfkgjdf");
|
let directory = String::from("ldfhgdfkgjdf");
|
||||||
::std::fs::remove_dir(&directory).map_err(chain_error_fn!(
|
::std::fs::remove_dir(&directory).map_err(chain_error_fn!(
|
||||||
|
@ -212,9 +242,64 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_works() -> Result<(), MyMainError> {
|
fn test_chain_error_fn() -> Result<(), MyMainError> {
|
||||||
let res = throw_error().map_err(chain_error_fn!(MyMainError, "I has an error."));
|
let res = throw_error().map_err(chain_error_fn!(MyMainError, "I has an error."));
|
||||||
|
|
||||||
|
if let Err(my_err) = res {
|
||||||
|
if let Some(source) = my_err.source() {
|
||||||
|
assert!(source.is::<MyError>());
|
||||||
|
}
|
||||||
|
println!("\nRoot cause is {:#?}\n", my_err.root_cause());
|
||||||
|
assert!(my_err.root_cause().unwrap().is::<::std::io::Error>());
|
||||||
|
assert!(my_err.find_cause::<::std::io::Error>().is_some());
|
||||||
|
|
||||||
|
if my_err.find_cause::<::std::io::Error>().is_some() {
|
||||||
|
println!("Has cause io::Error");
|
||||||
|
}
|
||||||
|
if my_err.find_cause::<MyError>().is_some() {
|
||||||
|
println!("Has cause MyError");
|
||||||
|
}
|
||||||
|
println!("-----------");
|
||||||
|
println!("Display Error:\n{}", my_err);
|
||||||
|
println!("-----------");
|
||||||
|
println!("Debug Error: \n{:#?}", my_err);
|
||||||
|
println!("-----------");
|
||||||
|
};
|
||||||
|
//res?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_into_chain_error_fn() -> Result<(), MyMainError> {
|
||||||
|
let res: Result<(), MyMainError> = throw_error().map_err(into_boxed_chain_error_fn!("I has an error."));
|
||||||
|
|
||||||
|
if let Err(my_err) = res {
|
||||||
|
if let Some(source) = my_err.source() {
|
||||||
|
assert!(source.is::<MyError>());
|
||||||
|
}
|
||||||
|
println!("\nRoot cause is {:#?}\n", my_err.root_cause());
|
||||||
|
assert!(my_err.root_cause().unwrap().is::<::std::io::Error>());
|
||||||
|
assert!(my_err.find_cause::<::std::io::Error>().is_some());
|
||||||
|
|
||||||
|
if my_err.find_cause::<::std::io::Error>().is_some() {
|
||||||
|
println!("Has cause io::Error");
|
||||||
|
}
|
||||||
|
if my_err.find_cause::<MyError>().is_some() {
|
||||||
|
println!("Has cause MyError");
|
||||||
|
}
|
||||||
|
println!("-----------");
|
||||||
|
println!("Display Error:\n{}", my_err);
|
||||||
|
println!("-----------");
|
||||||
|
println!("Debug Error: \n{:#?}", my_err);
|
||||||
|
println!("-----------");
|
||||||
|
};
|
||||||
|
//res?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_map_chain_err() -> Result<(), MyMainError> {
|
||||||
|
let res: Result<(), MyMainError> = throw_error().map_err(chain!());
|
||||||
|
|
||||||
if let Err(my_err) = res {
|
if let Err(my_err) = res {
|
||||||
if let Some(source) = my_err.source() {
|
if let Some(source) = my_err.source() {
|
||||||
assert!(source.is::<MyError>());
|
assert!(source.is::<MyError>());
|
||||||
|
|
Loading…
Reference in a new issue