miette icon indicating copy to clipboard operation
miette copied to clipboard

converting `impl Error` to `Diagnostic` implicitly when using with_err or cascading error using `?`

Open LordMZTE opened this issue 2 years ago • 5 comments

When using miette, I have this sort of code a lot:

use miette::{WithErr, Result};

fn main() -> Result<()> {
    // do_stuff returns a `Result<T, E>` where E implements the `Error` trait.
    do_stuff().into_diagnostic().with_err("failed to do stuff")?;

    // or without with_err
    do_stuff().into_diagnostic()?;

    Ok(())
}

and this results in many calls to into_diagnostic, which can get quite annoying. However, we could implement impl From<E: Error> for Report in order to be able to simplify the above code to something like this:

use miette::{IntoDiagnostic, WithErr, Result};

fn main() -> Result<()> {
    // do_stuff returns a `Result<T, E>` where E implements the `Error` trait.
    do_stuff().with_err("failed to do stuff")?;

    // or without with_err
    do_stuff()?;

    Ok(())
}

Is there any reason this is currently not possible? Has this just not been implemented or is doing this (in a binary crate) an antipattern? If so, how to do it better?

LordMZTE avatar Oct 05 '21 19:10 LordMZTE

It's unfortunate, but I can't make that go away because eyreish (the thing that makes miette::Result work) has to specialize on Diagnostic, not Error under the hood. And there's no way to handle both that I could figure out.

The plan for this particular wart is that it'll go away once provide-any is stabilized, since we'll be able to yank out eyreish altogether and miette will be able to provide a plain ol' EyreHandler to do all its fancy printing, instead, at that point :)

Sorry for the hassle. I've found that having a lot of .into_diagnostic()s is a good sign, for me, that maybe I should just make my own error type using thiserror, with #[from] impls, but I know that's also a bit more of a hassle than someone might want out of something like miette::Result that's meant to remove exactly that kind of extra work?

zkat avatar Oct 05 '21 21:10 zkat

Thanks for the explanation! I do think that this would make miette a whole lot more convenient to use, so I think it's a great idea to change this.

LordMZTE avatar Oct 05 '21 21:10 LordMZTE

If you have any ideas on what could actually work to remove this .into_diagnostic() thing, I would LOVE to hear them, btw!

zkat avatar Oct 05 '21 22:10 zkat

I've not taken a close look at miette's codebase yet, so I'm not sure I'll have any ideas. Once we do come up with a plan on how to go on with this, I'd be happy to contribute tho!

LordMZTE avatar Oct 05 '21 22:10 LordMZTE

Hi there, was there any change since 2021 making this more realistic to implement by now? I'm currently evaluating the use of miette for my work, but not having the easy ? is a extremely annoying and bloats the already verbose rust code even more. (Don't get me wrong, love the explicitness of rust, but at this point I am basically doing a Ctrl-H "?" -> ".into_diagnostic()?".)

Isn't this a beauty :D

image

Christoph-AK avatar Feb 28 '23 21:02 Christoph-AK