chainerror/examples/tutorial5.rs
Harald Hoyer 82257c881a
docs: use new names
Signed-off-by: Harald Hoyer <harald@hoyer.xyz>
2023-07-28 16:22:00 +02:00

34 lines
786 B
Rust

use chainerror::Context as _;
use std::error::Error;
use std::io;
fn do_some_io() -> Result<(), Box<dyn Error + Send + Sync>> {
Err(io::Error::from(io::ErrorKind::NotFound))?;
Ok(())
}
fn func2() -> Result<(), Box<dyn Error + Send + Sync>> {
let filename = "foo.txt";
do_some_io().context(format!("Error reading '{}'", filename))?;
Ok(())
}
fn func1() -> Result<(), Box<dyn Error + Send + Sync>> {
if let Err(e) = func2() {
if let Some(s) = e.source() {
eprintln!("func2 failed because of '{}'", s);
Err(e).context("func1 error")?;
}
}
Ok(())
}
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
if let Err(e) = func1() {
eprintln!("{}", e);
std::process::exit(1);
}
Ok(())
}