2023-07-28 16:21:22 +02:00
|
|
|
use chainerror::{Context as _, ErrorDown};
|
|
|
|
|
2018-12-20 10:08:54 +01:00
|
|
|
use std::error::Error;
|
|
|
|
use std::io;
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
2023-07-28 16:21:22 +02:00
|
|
|
chainerror::str_context!(Func2Error);
|
2018-12-20 10:08:54 +01:00
|
|
|
|
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-09-01 21:03:01 +02:00
|
|
|
do_some_io().context(Func2Error(format!("Error reading '{}'", filename)))?;
|
2018-12-20 10:08:54 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-07-28 16:21:22 +02:00
|
|
|
chainerror::str_context!(Func1ErrorFunc2);
|
|
|
|
chainerror::str_context!(Func1ErrorIO);
|
2018-12-20 10:08:54 +01:00
|
|
|
|
2020-03-03 14:37:11 +01:00
|
|
|
fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
|
2023-07-28 16:32:46 +02:00
|
|
|
func2().context(Func1ErrorFunc2::new("func1 error calling func2"))?;
|
2018-12-20 10:08:54 +01:00
|
|
|
let filename = "bar.txt";
|
2020-09-01 21:03:01 +02:00
|
|
|
do_some_io().context(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() {
|
2023-07-28 16:21:22 +02:00
|
|
|
if let Some(s) = e.downcast_ref::<chainerror::Error<Func1ErrorIO>>() {
|
2018-12-20 10:08:54 +01:00
|
|
|
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);
|
|
|
|
}
|
2020-09-01 21:03:01 +02:00
|
|
|
std::process::exit(1);
|
2018-12-20 10:08:54 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|