miette icon indicating copy to clipboard operation
miette copied to clipboard

Panic when trying to debug print `miette::Report`

Open sudw1n opened this issue 2 years ago • 1 comments

So, I was trying to find a way to get the pretty print output without having to return the error to main() (which was the only way I could find, even in the docs, to get the pretty print). To give more context, here's what I'm trying to do:

I have a function run() which returns miette::Result<()>. This function is called within another function run_prompt() which also returns miette::Result<()> but attaches a SourceCode to the returned error. Finally, run_prompt() is called by main() and the errors just propagate up until main() returns and my program exits.

However, for some errors I just want to print them to stderr without making the program exit (which happens if I let the error propagate to main()). Trying to just display the error (i.e. by doing eprintln!("{}", e)) doesn't give me any of the fancy output.

So I tried debug printing the error report:

pub fn run() -> miette::Result<()>
{
    // ... other stuff ...

    // some_fallible_function() returns a
    // Result<(), MyErrorEnum>.
    // MyErrorEnum is just an enum with
    // derive macros from `thiserror` and `miette`
    // on it
    let value = some_fallible_function()?;

    // ... other stuff ...
}

pub fn run_prompt() -> miette::Result<()>
{
    let line: String; // `line` is just a String

    // ... other stuff ...

     if let Err(e) = run(line.clone()).map_err(|err| err.with_source_code(line)) {
        eprintln!("Error: {:?}", e);
     }

     // ... other stuff ...
}

This does give me the fancy error print. However, the program immediately panics with a failed printing to stderr: formatter error message, as shown in the screenshot.

panic output

Here's the backtrace:

stack backtrace:
   0: rust_begin_unwind
             at /rustc/65ea825f4021eaf77f1b25139969712d65b435a4/library/std/src/panicking.rs:619:5
   1: core::panicking::panic_fmt
             at /rustc/65ea825f4021eaf77f1b25139969712d65b435a4/library/core/src/panicking.rs:72:14
   2: std::io::stdio::print_to
             at /rustc/65ea825f4021eaf77f1b25139969712d65b435a4/library/std/src/io/stdio.rs:1019:9
   3: std::io::stdio::_eprint
             at /rustc/65ea825f4021eaf77f1b25139969712d65b435a4/library/std/src/io/stdio.rs:1106:5
   4: lox::run_prompt
             at ./src/lib.rs:51:13
   5: lox::main
             at ./src/bin/lox.rs:15:13
   6: core::ops::function::FnOnce::call_once
             at /rustc/65ea825f4021eaf77f1b25139969712d65b435a4/library/core/src/ops/function.rs:250:5

The full backtrace was just too long, so I've omitted it for now.

I'm pretty sure the fault is on my part, as I'm very new to Rust and haven't fully understood how miette works. But I thought I should report this, since it might help improve the documentation. : )

sudw1n avatar Sep 23 '23 04:09 sudw1n

debug-printing is indeed the right way to display a miette diagnostic before exiting main, I have a very similar line in some code I wrote that does work. However, that does not change the fact that your code is panicking... I cannot see any miette function in the backtrace, at least the part that is here, but miette could be involved. Do you still have the code that caused this, and can you still trigger it? Maybe we can do some more investigation into why this is happening.

jdonszelmann avatar Nov 10 '23 00:11 jdonszelmann