zkat / miette

Fancy extension for std::error::Error with pretty, detailed diagnostic printing.
https://docs.rs/miette
Apache License 2.0
1.98k stars 113 forks source link

Thiserror W/ Miette how to display? #322

Closed gluax closed 8 months ago

gluax commented 10 months ago

Hey all,

I'm trying to leverage miette's pretty printing with thiserror.

But I run into the case where when an error bubbles up, it only displays what's written in the error proc macro for an enum, and it doesn't display the source code blocks or anything.

Example

#[derive(Debug, miette::Diagnostic, thiserror::Error)]
enum Error {
    #[error("make command failed")]
    #[diagnostic(code(foo::command_make_error))]
    MakeCmd {
        cmd: &'static str,
        #[source_code]
        stderr: String,
    },
}

Produces an error like this, not printing the source_code block at all:

image

I tried looking at the readme and other examples, but cannot figure out why this is the case.

Can someone explain how to make this work with thiserror?

zkat commented 10 months ago

https://github.com/zkat/miette?tab=readme-ov-file#-snippets you need to use labels

gluax commented 10 months ago

Ah, I see, thank you!!

So I can't attach multiple instances of arbitrary text(Strings/&'static str/etc) to be printed out, or have the code be dynamically set from a field?

zkat commented 8 months ago

Correct. You always need SourceCode + at least one label. But you can have a single label that's the span of your entire arbitrary string, if you want. Miette can do multiline spans