rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.15k stars 12.69k forks source link

Tracking Issue for `error::Report` #90172

Open seanchen1991 opened 3 years ago

seanchen1991 commented 3 years ago

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

pub struct Report<E = Box<dyn Error>> {
    error: E,
    show_backtrace: bool,
    pretty: bool,
}

impl<E> Report<E>
where
    Report<E>: From<E>,
{
    pub fn new(error: E) -> Report<E>;
}

impl<E> Report<E> {
    pub fn pretty(mut self, pretty: bool) -> Self;
    pub fn show_backtrace(mut self, show_backtrace: bool) -> Self;
}

impl<E> From<E> for Report<E>
where
    E: Error;

impl<'a, E> From<E> for Report<Box<dyn Error + 'a>>
where
    E: Error + 'a;

impl<E> fmt::Display for Report<E>
where
    E: Error;

impl fmt::Display for Report<Box<dyn Error>>;

impl<E> fmt::Debug for Report<E>
where
    Report<E>: fmt::Display;

Unresolved Questions

Edit(yaahc):

yaahc commented 2 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.

yaahc commented 2 years ago

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.

HTGAzureX1212 commented 2 years ago

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?

HTGAzureX1212 commented 2 years ago

After some brief investigation, such an implementation would not work due to a conflicting blanket implementation in core.

HTGAzureX1212 commented 2 years ago

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))