use std::error::Error; use std::io; use std::result::Result; use chainerror::prelude::v1::*; fn do_some_io() -> Result<(), Box> { Err(io::Error::from(io::ErrorKind::NotFound))?; Ok(()) } fn func3() -> Result<(), Box> { let filename = "foo.txt"; do_some_io().cherr(format!("Error reading '{}'", filename))?; Ok(()) } derive_str_cherr!(Func2Error); fn func2() -> ChainResult<(), Func2Error> { func3().cherr(Func2Error(format!("func2 error: calling func3")))?; Ok(()) } enum Func1Error { Func2, IO(String), } impl ::std::fmt::Display for Func1Error { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { match self { Func1Error::Func2 => write!(f, "func1 error calling func2"), Func1Error::IO(filename) => write!(f, "Error reading '{}'", filename), } } } impl ::std::fmt::Debug for Func1Error { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { write!(f, "{}", self) } } fn func1() -> ChainResult<(), Func1Error> { func2().cherr(Func1Error::Func2)?; let filename = String::from("bar.txt"); do_some_io().cherr(Func1Error::IO(filename))?; Ok(()) } fn main() { if let Err(e) = func1() { match e.kind() { Func1Error::Func2 => eprintln!("Main Error Report: func1 error calling func2"), Func1Error::IO(filename) => { eprintln!("Main Error Report: func1 error reading '{}'", filename) } } if let Some(e) = e.find_chain_cause::() { eprintln!("\nError reported by Func2Error: {}", e) } if let Some(e) = e.root_cause() { let ioerror = e.downcast_ref::().unwrap(); eprintln!("\nThe root cause was: std::io::Error: {:#?}", ioerror); } eprintln!("\nDebug Error:\n{:?}", e); eprintln!("\nAlternative Debug Error:\n{:#?}", e); } }