0

I know that error conditions can be propagated up the call stack with '?', but at some point the errors need to be handled or generate a panic. I don't want users to see a panic, so I am trying to give more helpful messages for each case.

For example, in a simple application I have main function doing a bunch of file I/O. For each open/create/read/write function I end up with very similar code to handle errors...

 let mut infile = File::open(&args.infile)
        .with_context(|| format!("Failed to open file '{}'", &args.infile.display()))?;
    let mut outfile = File::create(&args.outfile)
        .with_context(|| format!("Failed to create file '{}'", &args.outfile.display()))?;

    // We need the length of the source data
    let length = infile
        .seek(SeekFrom::End(0))
        .with_context(|| format!("Could not seek in file '{}", &args.infile.display()))?;
    infile
        .seek(SeekFrom::Start(0))
        .with_context(|| format!("Could not seek in file '{}", &args.infile.display()))?;

    // Write a header to the file
    writeln!(outfile, "SOME HEADER TEXT HERE")
        .with_context(|| format!("Could not write to file '{}", &args.outfile.display()))?;
...

so there is a with_context (from the anyhow crate) for every file operation. The results are much nicer than panics, but is there a better way to do this in Rust? One of the things I like about Python is that you can put a series of operations like this in a try block and handle all the errors at the end and I am just wondering if there is a Rust equivalent?

rdanter
  • 131
  • 6
  • I don't understand. Do you want to handle the errors inside the function (the equivalent of `try..except` in Python)? – Chayim Friedman Jun 15 '22 at 11:35
  • Ideally yes. The goal is to avoid having to handle errors on every I/O call individually, but group the error handling in one place, if that is possible in Rust. It's OK if it isn't possible, I have been handling errors in C code individually like the Rust example for many years, but if Rust has a better way then I'd like to know. – rdanter Jun 15 '22 at 12:09
  • But you are adding different context to each error. – Chayim Friedman Jun 15 '22 at 12:11
  • I have been following the Rust Command Line book which uses anyhow and with_context, open to suggestions for a better approach – rdanter Jun 15 '22 at 12:19
  • No, this is good, I'm just wondering how you can handle them together. – Chayim Friedman Jun 15 '22 at 12:20
  • Is this what you are looking for: https://stackoverflow.com/q/55755552/5397009 ? – Jmb Jun 15 '22 at 13:22
  • Hmm, that looks interesting, I will it. – rdanter Jun 15 '22 at 13:58

0 Answers0