From 4bcf38dddae91928d877fce2c02f1a8db9546c6a Mon Sep 17 00:00:00 2001 From: andylokandy Date: Tue, 3 Feb 2026 14:50:32 +0800 Subject: [PATCH] chore: update custom layout example --- examples/src/custom-layout.rs | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/examples/src/custom-layout.rs b/examples/src/custom-layout.rs index c8abce1..4311066 100644 --- a/examples/src/custom-layout.rs +++ b/examples/src/custom-layout.rs @@ -50,26 +50,18 @@ impl Error for MainError {} impl MainError { /// Convert an `Exn` into MainError with custom numbered list formatting. pub fn new(err: Exn) -> Self { - fn collect_frames(frame: &Frame, frames: &mut Vec) { - // Add this frame first - frames.push(format!("[{}] {}", frame.location(), frame.error())); - // Then collect children + fn collect_frames(report: &mut String, i: usize, frame: &Frame) { + if i > 0 { + report.push('\n'); + } + write!(report, "{}: {}, at {}", i, frame.error(), frame.location()).unwrap(); for child in frame.children() { - collect_frames(child, frames); + collect_frames(report, i + 1, child); } } - let mut frames = vec![]; - collect_frames(err.frame(), &mut frames); - - // Format as numbered list let mut report = String::new(); - for (i, frame) in frames.iter().enumerate() { - if i > 0 { - writeln!(&mut report).unwrap(); - } - write!(&mut report, "{i}: {frame}").unwrap(); - } + collect_frames(&mut report, 0, err.frame()); MainError(report) } @@ -121,5 +113,5 @@ mod http { // Output when running `cargo run --example custom_layout`: // // Error: fatal error occurred in application: -// 0: [examples/src/custom-layout.rs:82:30] failed to run app -// 1: [examples/src/custom-layout.rs:102:9] failed to send request to server: https://example.com +// 0: failed to run app, at examples/src/custom-layout.rs:74:30 +// 1: failed to send request to server: https://example.com, at examples/src/custom-layout.rs:94:9