diff --git a/lnprototest/errors.py b/lnprototest/errors.py index 5e24cf7..0319ea6 100644 --- a/lnprototest/errors.py +++ b/lnprototest/errors.py @@ -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 + return "" + 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()}" diff --git a/lnprototest/event.py b/lnprototest/event.py index 7172011..ed41e63 100644 --- a/lnprototest/event.py +++ b/lnprototest/event.py @@ -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()}" + raise EventError(self, error_msg) break return True