Preferred way to use anyhow::Error with tor_error::Report.
Right now we can't use an anyhow::Error with warn_report!
, as in warn_report!(err, "Bad vibes")
. This is (mainly) because anyhow::Error
doesn't implement std::error::Error
, and so we can't use the existing implementation of ErrorReport
for it. We also cannot give anyhow::Error
its own implementation of ErrorReport
, since Rust complains that a later version of an upstream crate might cause anyhow::Error
to implement StdError
.
I've found (and tested) four approaches to this so far.
1. Use an extra-trait kludge.
Define a new ErrorReportHack
trait with the same signature as ErrorReport
. Implement ErrorReportHack for anyhow::Error. In the event_report!()
macro, import both ErrorReport and ErrorReportHack.
This will fail with an ambiguous-method-resolution error if anyhow::Error
ever implements StdError
, but Rust doesn't complaim about that.
See https://gitlab.torproject.org/nickm/arti/-/commits/anyhow-report-v1 for this approach.
2. Explicit wrapping
Define a wrapper type for anyhow::Error, and just stick anyhow::Error in that explicitly before passing it to warn_report. I used Report, but there's no reason you'd have to do that.
This is ugly, but won't spread outside of arti
since we don't use anyhow::Error elsewhere. (Right?)
See https://gitlab.torproject.org/nickm/arti/-/commits/anyhow-report-v2 for this approach.
3. Don't use the tor_error macros.
Just call warn!("Bad vibes: {}", tor_report::Report(err))
explicitly.
Again, this isn't so bad, since we don't use anyhow::Error everywhere.
https://gitlab.torproject.org/nickm/arti/-/commits/anyhow-report-v3
4. Define a new warn_report_anyhow macro.
This is the same as 2, but we add a new macro to work with anyhow::Error
and nothing else. (Also, this time I defined a separate (hidden) wrapper type.)
See https://gitlab.torproject.org/nickm/arti/-/commits/anyhow-report-v4
Personally, I think that option 3 might be the most reasonable, and option 1 might be in some sense the "prettiest". I tried about 6 other things that didn't work.
@Diziet, which approach do you prefer, if any?