Open seanchen1991 opened 3 years ago
Regarding the first unresolved issue about making it convert the inner type, I don't think its possible to do it generally, but I was able to add a special case for Box<dyn Error>
https://github.com/rust-lang/rust/pull/91938/files#diff-6605ea65189fd18a801464d4718ad307bb93be455d1592603ebf9382063902b2R1388 so that it boxes and wraps the error at the same time, which should make it work nicely as the return type for main
.
I want to raise a concern on this related to naming. I think it's not unlikely that we may want to have a Box<dyn Error>
equivalent type in std
at some point for storing errors which are only meant to be reported, similar to anyhow::Error
or eyre::Report
. If we do introduce such a type in the future it might make more sense for that type to be the one named Report
, or something else that clearly communicates that it semantically represents an error which is no longer expected to be handled anymore other than via reporting.
It might make more sense to call this std::error::Reporter
or something along those lines. For now though I think it's fine to leave it as is. I'm in no rush to stabilize this interface.
Not sure whether it would be in scope of this issue, but would it be possible for a impl<E, F> From<Report<F>> for Report<E> where E: Error + From<F>, F: Error
be added such that Report<E>
can be used with the ?
operator if I had to change error types when creating a report from an error?
After some brief investigation, such an implementation would not work due to a conflicting blanket implementation in core.
A little bit more context for the rationale, I encountered this error when trying to convert a Reoprt<F>
to a Report<E>
given E: From<F>
:
error[E0277]: `?` couldn't convert the error to `Report<GatewayError>`
--> hartex-gateway\src\main.rs:36:34
|
36 | dotenv::dotenv().map_report()?;
| ^ the trait `From<Report<hartex_core::dotenv::Error>>` is not implemented for `Report<GatewayError>`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= help: the following other types implement trait `From<T>`:
Report<Box<(dyn std::error::Error + 'a)>>
Report<E>
= note: required because of the requirements on the impl of `FromResidual<Result<Infallible, Report<hartex_core::dotenv::Error>>>` for `Result<(), Report<GatewayError>>`
map_report
is a trait extension function I added to Result<T, E>
that does this:
self
.map_err(E::from)
.map_err(Report::new)
.map_err(|report| report.pretty(true).show_backtrace(true))
Feature gate:
#![feature(error_reporter)]
This is a tracking issue for a
Report
type that wraps an error, allowing the entire error chain to be printed in either single- or multi-line format.Implementation PR: https://github.com/rust-lang/rust/pull/91938
Public API
Unresolved Questions
impl<E> From<E> for Report<E> where E: Error
that converts an error into a report. Is it possible to make this work for types that convert into the inner error type?Edit(yaahc):
show_backtrace
currently has no effect if thepretty
format isn't enabled, we won't display backtraces ever in the single line format. I dislike this aspect, and am wondering if settingshow_backtrace
should implicitly also enablepretty
, which isn't necessarily much better, but at least it's less error prone.pretty
, in the docs we refer to it as the multi-line format, should it just bemultiline
?pretty
/show_backtrace
means you can't write something likelet val = my_result.map_err(Report::new).map_err(Report::pretty).unwrap();
, though I'm leaning towards this being the lesser evil compared to the issues with lifetimes and if statements on alternative builder API styles. It's not that big of a deal to introduce a closure like.map_err(|r| r.pretty(should_be_pretty))