2020-08-28 16:45:24 +02:00
|
|
|
use chainerror::prelude::v1::*;
|
2018-12-20 10:08:54 +01:00
|
|
|
use std::error::Error;
|
|
|
|
use std::io;
|
|
|
|
use std::result::Result;
|
|
|
|
|
2020-03-03 14:37:11 +01:00
|
|
|
fn do_some_io() -> Result<(), Box<dyn Error + Send + Sync>> {
|
2018-12-20 10:08:54 +01:00
|
|
|
Err(io::Error::from(io::ErrorKind::NotFound))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
derive_str_cherr!(Func2Error);
|
|
|
|
|
2020-03-03 14:37:11 +01:00
|
|
|
fn func2() -> Result<(), Box<dyn Error + Send + Sync>> {
|
2018-12-20 10:08:54 +01:00
|
|
|
let filename = "foo.txt";
|
2020-08-28 16:45:24 +02:00
|
|
|
do_some_io().cherr(Func2Error(format!("Error reading '{}'", filename)))?;
|
2018-12-20 10:08:54 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
derive_str_cherr!(Func1ErrorFunc2);
|
|
|
|
derive_str_cherr!(Func1ErrorIO);
|
|
|
|
|
2020-03-03 14:37:11 +01:00
|
|
|
fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
|
2020-08-28 16:45:24 +02:00
|
|
|
func2().cherr(Func1ErrorFunc2(format!("func1 error calling func2")))?;
|
2018-12-20 10:08:54 +01:00
|
|
|
let filename = "bar.txt";
|
2020-08-28 16:45:24 +02:00
|
|
|
do_some_io().cherr(Func1ErrorIO(format!("Error reading '{}'", filename)))?;
|
2018-12-20 10:08:54 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:37:11 +01:00
|
|
|
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
|
2018-12-20 10:08:54 +01:00
|
|
|
if let Err(e) = func1() {
|
|
|
|
if let Some(s) = e.downcast_ref::<ChainError<Func1ErrorIO>>() {
|
|
|
|
eprintln!("Func1ErrorIO:\n{:?}", s);
|
|
|
|
}
|
|
|
|
|
2018-12-21 13:50:08 +01:00
|
|
|
if let Some(s) = e.downcast_chain_ref::<Func1ErrorFunc2>() {
|
2018-12-20 10:08:54 +01:00
|
|
|
eprintln!("Func1ErrorFunc2:\n{:?}", s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|