From 48f5dab744d2316c77d115ff08be1cf9e66d7875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Thu, 18 May 2023 17:23:38 +0200 Subject: [PATCH] Do not print errors recursively MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcel Müller --- src/handlers/graphical.rs | 1 + tests/test_diagnostic_source_macro.rs | 56 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/handlers/graphical.rs b/src/handlers/graphical.rs index 88ec9e8..b5dd754 100644 --- a/src/handlers/graphical.rs +++ b/src/handlers/graphical.rs @@ -261,6 +261,7 @@ impl GraphicalReportHandler { // Don't print footer for inner errors let mut inner_renderer = self.clone(); inner_renderer.footer = None; + inner_renderer.with_cause_chain = false; inner_renderer.render_report(&mut inner, diag)?; writeln!(f, "{}", textwrap::fill(&inner, opts))?; diff --git a/tests/test_diagnostic_source_macro.rs b/tests/test_diagnostic_source_macro.rs index 327b6c2..536aedf 100644 --- a/tests/test_diagnostic_source_macro.rs +++ b/tests/test_diagnostic_source_macro.rs @@ -135,3 +135,59 @@ fn test_diagnostic_source_is_output() { assert_eq!(expected, out); } + +#[derive(Debug, miette::Diagnostic, thiserror::Error)] +#[error("A nested error happened")] +struct NestedError { + #[source_code] + code: String, + #[label("here")] + label: (usize, usize), + #[diagnostic_source] + the_other_err: Box, +} + +#[test] +fn test_nested_diagnostic_source_is_output() { + let inner_error = TestStructError { + asdf_inner_foo: SourceError { + code: String::from("This is another error"), + help: String::from("You should fix this"), + label: (3, 4), + }, + }; + let diag = NestedError { + code: String::from("right here"), + label: (6, 4), + the_other_err: Box::new(inner_error), + }; + let mut out = String::new(); + miette::GraphicalReportHandler::new_themed(miette::GraphicalTheme::unicode_nocolor()) + .with_width(80) + .with_footer("Yooo, a footer".to_string()) + .render_report(&mut out, &diag) + .unwrap(); + println!("{}", out); + + let expected = r#" × A nested error happened + ├─▶ × TestError + │ + ╰─▶ × A complex error happened + ╭──── + 1 │ This is another error + · ──┬─ + · ╰── here + ╰──── + help: You should fix this + + ╭──── + 1 │ right here + · ──┬─ + · ╰── here + ╰──── + + Yooo, a footer +"#; + + assert_eq!(expected, out); +}