Skip to content

Commit 52f9de8

Browse files
authored
feat(tonic): Improve Status formatting (#2403)
## Motivation The `Status` struct is the canonical error message in `tonic`. Printing it currently prints out a very verbose and difficult-to-read message. ## Solution Improve `impl, Display for status` by: * Only printing the `message` if it is non-empty * Only printing the `metadata` if it is non-empty` * Always omitting the binary `details` (printing out `details: [22, 51, 50, 48, 51, 53, 98, 57, 50, 55, 50, 55, 100, 54, 101, …]` is not helping anyone)
1 parent efd69a2 commit 52f9de8

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

tonic/src/status.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -748,14 +748,16 @@ impl From<std::io::Error> for Status {
748748

749749
impl fmt::Display for Status {
750750
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
751-
write!(
752-
f,
753-
"status: {:?}, message: {:?}, details: {:?}, metadata: {:?}",
754-
self.code(),
755-
self.message(),
756-
self.details(),
757-
self.metadata(),
758-
)
751+
write!(f, "status: '{}'", self.code())?;
752+
753+
if !self.message().is_empty() {
754+
write!(f, ", self: {:?}", self.message())?;
755+
}
756+
// We intentionally omit `self.details` since it's binary data, not fit for human eyes.
757+
if !self.metadata().is_empty() {
758+
write!(f, ", metadata: {:?}", self.metadata().as_ref())?;
759+
}
760+
Ok(())
759761
}
760762
}
761763

0 commit comments

Comments
 (0)