You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
pubfnrun() -> miette::Result<()>{// ... other stuff ...// some_fallible_function() returns a// Result<(), MyErrorEnum>.// MyErrorEnum is just an enum with// derive macros from `thiserror` and `miette`// on itlet value = some_fallible_function()?;// ... other stuff ...}pubfnrun_prompt() -> miette::Result<()>{let line:String;// `line` is just a String// ... other stuff ...ifletErr(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.
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. : )
The text was updated successfully, but these errors were encountered:
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.
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 returnsmiette::Result<()>
. This function is called within another functionrun_prompt()
which also returnsmiette::Result<()>
but attaches aSourceCode
to the returned error. Finally,run_prompt()
is called bymain()
and the errors just propagate up untilmain()
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 tomain()
). Trying to just display the error (i.e. by doingeprintln!("{}", e)
) doesn't give me any of the fancy output.So I tried debug printing the error report:
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.Here's the backtrace:
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. : )The text was updated successfully, but these errors were encountered: