From 7d249143cc8ce211b91d8bc1d2f6edc3bf7b9cda Mon Sep 17 00:00:00 2001 From: arnav-makkar <142909930+arnav-makkar@users.noreply.github.com> Date: Mon, 14 Apr 2025 23:05:55 +0530 Subject: [PATCH 1/2] improve error message --- lnprototest/errors.py | 21 +++++++++++++++++++++ lnprototest/event.py | 6 +++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lnprototest/errors.py b/lnprototest/errors.py index 5e24cf7b..17b647b9 100644 --- a/lnprototest/errors.py +++ b/lnprototest/errors.py @@ -20,7 +20,28 @@ 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 7172011f..2e3af653 100644 --- a/lnprototest/event.py +++ b/lnprototest/event.py @@ -374,7 +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 From 751b5f81896811ce0c2503ec8003acf9cf8c5ac5 Mon Sep 17 00:00:00 2001 From: arnav-makkar <142909930+arnav-makkar@users.noreply.github.com> Date: Tue, 15 Apr 2025 16:53:48 +0530 Subject: [PATCH 2/2] fix formatting as per Black style guidelines --- lnprototest/errors.py | 5 ++--- lnprototest/event.py | 3 +-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lnprototest/errors.py b/lnprototest/errors.py index 17b647b9..0319ea6e 100644 --- a/lnprototest/errors.py +++ b/lnprototest/errors.py @@ -25,7 +25,7 @@ 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') + 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})" @@ -35,13 +35,12 @@ def decode_hex_data(hex_data: str) -> str: def __str__(self) -> str: # Look for hex data in the message and try to decode it - parts = self.message.split('data=') + 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 2e3af653..ed41e63b 100644 --- a/lnprototest/event.py +++ b/lnprototest/event.py @@ -376,10 +376,9 @@ def action(self, runner: "Runner") -> bool: if err: # Format the error message to be more readable error_msg = f"Expected {self.msgtype}, got {msg.messagetype.name}" - if hasattr(msg, 'fields'): + if hasattr(msg, "fields"): error_msg += f": {msg.to_str()}" raise EventError(self, error_msg) - break return True