-
Notifications
You must be signed in to change notification settings - Fork 135
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
feat: implement Debug for PingError so messages display #1659
Conversation
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.
Few small comments.
.map_err(|_| { | ||
ssz::DecodeError::BytesInvalid(format!("Invalid utf8 string: {:?}", self.message)) | ||
}) | ||
.map_err(|_| std::fmt::Error)?; |
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.
Wouldn't this map_err
completely invalidate previous one and basically always return std::fmt::Error
?
How about we do something like this:
let message = String::from_utf8(&self.message)
.unwrap_or_else(|_| format!("Invalid utf8 string: {}", hex_encode(&self.message)));
f.debug_struct("PingError")
.field("error_code", &self.error_code)
.field("message", &message)
.finish()
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.
Good catch
@@ -6,7 +8,7 @@ use ssz_types::{typenum::U300, VariableList}; | |||
use crate::types::portal_wire::CustomPayload; | |||
|
|||
/// Used to respond to pings which the node can't handle | |||
#[derive(PartialEq, Debug, Clone, Encode, Decode)] | |||
#[derive(PartialEq, Clone, Encode, Decode)] |
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.
nit: In my opinion, if structure implements PartialEq
, it should also implement Eq
if it applies for it.
6655266
to
3ce5401
Compare
@@ -30,6 +32,18 @@ impl PingError { | |||
} | |||
} | |||
|
|||
impl Debug for PingError { | |||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |||
let message = String::from_utf8(self.message.to_vec()) |
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.
nit: you should be able to use &*self.message
here as well (instead of calling to_vec()
)
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.
That doesn't compile String::from_utf8
only takes a Vec
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.
&* gives you a slice
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.
Looks like milos was looking at str::from_utf8 well I was looking at String::from_utf8
What was wrong?
The default derived Debug implementation printed the message as bytes
How was it fixed?
Implement Debug manually to decode the Ping Message, there is no point in decoding it unless we print it so I think this strikes a pretty good balance.