Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions lnprototest/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,27 @@ def path_to_str(self) -> str:
result += f"{event},"
return f"{result}]"

@staticmethod
def decode_hex_data(hex_data: str) -> str:
"""Decode hex data into readable text if possible"""
try:
# Try to decode as ASCII/UTF-8
decoded = bytes.fromhex(hex_data).decode("utf-8", errors="replace")
# If the decoded text is mostly readable, return it
if all(ord(c) < 128 for c in decoded):
return f" (decoded: {decoded})"
except (ValueError, UnicodeDecodeError):
pass
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is really dangerous, please do not try to shoot yourself in the foot when you do not need it, we are already using Python.

return ""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not understanding whe need to return an empty string here, can you explainit to me?


def __str__(self) -> str:
# Look for hex data in the message and try to decode it
parts = self.message.split("data=")
if len(parts) > 1:
hex_data = parts[1].split()[0] # Get the hex data before any spaces
decoded = self.decode_hex_data(hex_data)
if decoded:
self.message = self.message.replace(hex_data, f"{hex_data}{decoded}")
return f"`{self.message}` on event {self.path_to_str()}"


Expand Down
7 changes: 5 additions & 2 deletions lnprototest/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,11 @@ def action(self, runner: "Runner") -> bool:

err = self.message_match(runner, msg)
if err:
raise EventError(self, "{}: message was {}".format(err, msg.to_str()))

# Format the error message to be more readable
error_msg = f"Expected {self.msgtype}, got {msg.messagetype.name}"
if hasattr(msg, "fields"):
error_msg += f": {msg.to_str()}"
Comment on lines +379 to +380
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not understand why we are checking the fields here, can you expand more on this part too?

raise EventError(self, error_msg)
break
return True

Expand Down