I don't mean compilation warnings, but issues at application-level logic at runtime, where something isn't quite right, but it's not as bad to be an error. Like Result
, but for warnings, not errors.
Rust programs are doing great with propagation of function-terminating errors, and there are many conventions for signaling and handling of errors.
But when I want to say "the operation succeeded, but I had to recover from some errors" or "here's the result, but some data was missing"?
The simplest approach is to do:
warn!("Improvising!");
but that's suitable only for some CLI applications. In a context of a web server, for example, I may want to print warnings in HTML, or in an HTTP header, or insert in a field of a JSON response.
I've tried returning warnings as part of the result:
fn doit() -> Result<(T, Vec<Warning>), Error> {}
but that was cumbersome to use when I wanted to aggregate warnings from several function calls:
let mut all_warnings = Vec::new();
loop {
let (res, mut tmp) = doit()?;
my_warnings.append(&mut tmp);
…
}
Ok(my_result, all_warnings)
I've also tried:
fn doit(warnings: &mut Vec<Warning>) -> Result<T, Error> {}
which was easier to use, but &mut
gets in the way of parallelization. So I ended up with:
fn doit(warnings: Arc<WarningsCollector>) -> Result<T, Error> {}
that does locking internally itself.
Has anyone been looking into this? Are there other, better approaches?