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(())
|
|
|
|
}
|
|
|
|
|
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(format!("Error reading '{}'", filename))?;
|
2018-12-20 10:08:54 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:37:11 +01:00
|
|
|
fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
|
2020-09-01 21:03:01 +02:00
|
|
|
func2().context("func1 error")?;
|
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() {
|
|
|
|
eprintln!("Error: {}", e);
|
2020-03-03 14:37:11 +01:00
|
|
|
let mut s: &(dyn Error) = e.as_ref();
|
2018-12-20 10:08:54 +01:00
|
|
|
while let Some(c) = s.source() {
|
|
|
|
if let Some(ioerror) = c.downcast_ref::<io::Error>() {
|
|
|
|
eprintln!("caused by: std::io::Error: {}", ioerror);
|
|
|
|
match ioerror.kind() {
|
|
|
|
io::ErrorKind::NotFound => eprintln!("of kind: std::io::ErrorKind::NotFound"),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintln!("caused by: {}", c);
|
|
|
|
}
|
|
|
|
s = c;
|
|
|
|
}
|
2020-09-01 21:03:01 +02:00
|
|
|
std::process::exit(1);
|
2018-12-20 10:08:54 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|