Skip to content
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

Merged
merged 2 commits into from
Feb 1, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::fmt::Debug;

use anyhow::anyhow;
use ssz::Encode;
use ssz_derive::{Decode, Encode};
use ssz_types::{typenum::U300, VariableList};

use crate::types::portal_wire::CustomPayload;
use crate::{types::portal_wire::CustomPayload, utils::bytes::hex_encode};

/// Used to respond to pings which the node can't handle
#[derive(PartialEq, Debug, Clone, Encode, Decode)]
#[derive(Eq, PartialEq, Clone, Encode, Decode)]
pub struct PingError {
pub error_code: u16,
pub message: VariableList<u8, U300>,
Expand All @@ -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())
Copy link
Collaborator

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())

Copy link
Member Author

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&* gives you a slice

Copy link
Member Author

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

.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()
}
}

impl From<PingError> for CustomPayload {
fn from(ping_error: PingError) -> Self {
CustomPayload::from(ping_error.as_ssz_bytes())
Expand Down