Skip to content

Commit

Permalink
Show source chain with alt format for Error (#949)
Browse files Browse the repository at this point in the history
For debugging purposes it can be useful to expose the full details of an
error, but this output is often too verbose to show in the default
Display format. The anyhow crate is a widely used example of this
approach, exposing the full backtrace chain when the `{:#}` alternate
formatting modifier is used.

Add an alternate Display formatting modifier for Error that will show
the full source chain. We skip the first source, as this is already
printed as part of the outermost error.
  • Loading branch information
wfchandler authored Oct 28, 2024
1 parent 09d0118 commit 3a49d01
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions progenitor-client/src/progenitor_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,31 +322,42 @@ where
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::InvalidRequest(s) => {
write!(f, "Invalid Request: {}", s)
write!(f, "Invalid Request: {}", s)?;
}
Error::CommunicationError(e) => {
write!(f, "Communication Error: {}", e)
write!(f, "Communication Error: {}", e)?;
}
Error::ErrorResponse(rve) => {
write!(f, "Error Response: ")?;
rve.fmt_info(f)
rve.fmt_info(f)?;
}
Error::InvalidUpgrade(e) => {
write!(f, "Invalid Response Upgrade: {}", e)
write!(f, "Invalid Response Upgrade: {}", e)?;
}
Error::ResponseBodyError(e) => {
write!(f, "Invalid Response Body Bytes: {}", e)
write!(f, "Invalid Response Body Bytes: {}", e)?;
}
Error::InvalidResponsePayload(b, e) => {
write!(f, "Invalid Response Payload ({:?}): {}", b, e)
write!(f, "Invalid Response Payload ({:?}): {}", b, e)?;
}
Error::UnexpectedResponse(r) => {
write!(f, "Unexpected Response: {:?}", r)
write!(f, "Unexpected Response: {:?}", r)?;
}
Error::PreHookError(s) => {
write!(f, "Pre-hook Error: {}", s)
write!(f, "Pre-hook Error: {}", s)?;
}
}

if f.alternate() {
use std::error::Error as _;

let mut src = self.source().and_then(|e| e.source());
while let Some(s) = src {
write!(f, ": {s}")?;
src = s.source();
}
}
Ok(())
}
}

Expand Down

0 comments on commit 3a49d01

Please sign in to comment.