-
-
Notifications
You must be signed in to change notification settings - Fork 44
feat: improve error printing #517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -171,7 +171,26 @@ impl Error { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl Debug for Error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Debug::fmt(&self.repr, f) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // If the alternate (`{:#?}`) formatting has been specified, print out the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // `Debug` formatting normally. If not, (which is the case when using | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // `Result::unwrap()` or `Result::expect()`) pretty print the error. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if f.alternate() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Debug::fmt(&self.repr, f) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Display::fmt(&self.repr.inner, f)?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writeln!(f)?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writeln!(f)?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writeln!(f, "caused by:")?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut source = self.source(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while let Some(e) = source { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writeln!(f, " {e}")?; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+180
to
+188
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Display::fmt(&self.repr.inner, f)?; | |
| writeln!(f)?; | |
| writeln!(f)?; | |
| writeln!(f, "caused by:")?; | |
| let mut source = self.source(); | |
| while let Some(e) = source { | |
| writeln!(f, " {e}")?; | |
| // Print the root error message. | |
| Display::fmt(&self.repr.inner, f)?; | |
| // Prepare to print the source chain. | |
| let root_msg = format!("{}", &self.repr.inner); | |
| writeln!(f)?; | |
| writeln!(f)?; | |
| writeln!(f, "caused by:")?; | |
| let mut source = self.source(); | |
| let mut is_first = true; | |
| while let Some(e) = source { | |
| let msg = format!("{}", e); | |
| // For status-propagation wrappers, the first source may have the | |
| // same message as the root error; skip it to avoid duplication. | |
| if !is_first || msg != root_msg { | |
| writeln!(f, " {msg}")?; | |
| } | |
| is_first = false; |
Copilot
AI
Mar 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This always prints a blank-line separator and a "caused by:" section even when self.source() is None, which will produce output ending with an empty "caused by:" block. Consider only emitting the extra newlines and the "caused by" header when at least one source exists.
| writeln!(f)?; | |
| writeln!(f)?; | |
| writeln!(f, "caused by:")?; | |
| let mut source = self.source(); | |
| while let Some(e) = source { | |
| writeln!(f, " {e}")?; | |
| source = e.source(); | |
| let mut source = self.source(); | |
| if source.is_some() { | |
| writeln!(f)?; | |
| writeln!(f)?; | |
| writeln!(f, "caused by:")?; | |
| while let Some(e) = source { | |
| writeln!(f, " {e}")?; | |
| source = e.source(); | |
| } |
Copilot
AI
Mar 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pretty-printing logic in Debug::fmt is new user-facing behavior but there are no tests asserting the {:#?} vs {:?} formatting, the presence/absence of the "caused by" block, and the formatting of multi-level source chains. Adding unit tests in this file’s tests module would help prevent regressions (especially around Error::internal/Error::with_status).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that should be within the
whileloop to show the chain of causalityUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.