feat: improve error printing#517
Conversation
There was a problem hiding this comment.
Pull request overview
Updates cot-core::Error’s Debug formatting so that {:?} produces a human-friendly error message plus its source chain (similar to anyhow), while {:#?} retains the original structured debug output. This improves the default panic output from Result::unwrap() / expect() in cot::main.
Changes:
- Implement non-alternate
Debugformatting to print the error viaDisplayplus a “caused by” source chain. - Preserve the prior
Debugoutput behind alternate formatting ({:#?}).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
| Branch | improve-errors |
| Testbed | github-ubuntu-latest |
Click to view all benchmark results
| Benchmark | Latency | Benchmark Result microseconds (µs) (Result Δ%) | Upper Boundary microseconds (µs) (Limit %) |
|---|---|---|---|
| empty_router/empty_router | 📈 view plot 🚷 view threshold | 6,480.70 µs(+6.36%)Baseline: 6,093.20 µs | 7,677.76 µs (84.41%) |
| json_api/json_api | 📈 view plot 🚷 view threshold | 1,053.70 µs(-1.92%)Baseline: 1,074.37 µs | 1,323.65 µs (79.61%) |
| nested_routers/nested_routers | 📈 view plot 🚷 view threshold | 971.58 µs(-2.09%)Baseline: 992.35 µs | 1,204.08 µs (80.69%) |
| single_root_route/single_root_route | 📈 view plot 🚷 view threshold | 939.38 µs(-1.62%)Baseline: 954.82 µs | 1,167.50 µs (80.46%) |
| single_root_route_burst/single_root_route_burst | 📈 view plot 🚷 view threshold | 16,969.00 µs(-4.26%)Baseline: 17,724.56 µs | 21,190.21 µs (80.08%) |
| writeln!(f)?; | ||
| writeln!(f)?; | ||
| writeln!(f, "caused by:")?; |
There was a problem hiding this comment.
I think that should be within the while loop to show the chain of causality
There was a problem hiding this comment.
The Rust stack traces are printed with 0:, 1:, 2:, etc. at the beginning of each frame. Maybe that would do? I don't think I'd like to inflate the error messages with excessive newlines.
Codecov Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
seqre
left a comment
There was a problem hiding this comment.
Please make cargo insta test for this functionality
When using `Result::expect()`, (e.g. in the default `cot::main`) errors are printed using `Debug` printer. This causes very ugly errors to be produced. Instead, when the alternate mode is not being used, just format the error nicely, along with the error source chain. This roughly duplicates the `anyhow::Error` error printing logic: https://docs.rs/anyhow/latest/anyhow/struct.Error.html#method.chain
| let error = self.inner(); | ||
| Display::fmt(error, f)?; | ||
|
|
||
| if let Some(source) = error.source() { |
| if f.alternate() { | ||
| Debug::fmt(&self.repr, f) |
| let mut source = Some(source); | ||
| let mut i = 0; | ||
| while let Some(e) = source { | ||
| writeln!(f, "{i:4}: {e}")?; | ||
|
|
||
| source = e.source(); | ||
| i += 1; |
| // `Debug` formatting normally. If not, (which is the case when using | ||
| // `Result::unwrap()` or `Result::expect()`) pretty print the error. |
When using
Result::expect(), (e.g. in the defaultcot::main) errors are printed usingDebugprinter. This causes very ugly errors to be produced. Instead, when the alternate mode is not being used, just format the error nicely, along with the error source chain.This roughly duplicates the
anyhow::Errorerror printing logic: https://docs.rs/anyhow/latest/anyhow/struct.Error.html#method.chainThe change turns this:

into this:
