adding error context

This commit is contained in:
Fabio Montefuscolo 2025-04-02 06:50:39 +02:00
parent 2d5ac33cf9
commit baf6df0fe7
Signed by: fabiomontefuscolo
GPG key ID: 7598676DAE19B4EF

View file

@ -6,8 +6,13 @@ struct Cli {
path: std::path::PathBuf,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let content = std::fs::read_to_string("test.txt")?;
#[derive(Debug)]
struct CustomError(String);
fn main() -> Result<(), CustomError> {
let path = "test.txt";
let content = std::fs::read_to_string(path)
.map_err(|err| CustomError(format!("Error reading `{}`: {}", path, err)))?;
println!("file content: {}", content);
Ok(())
}