Repository: the-hive-lab/pydwm1001 (language: python)

Issue:
Improve parsing exception messages for `Listener` class
There are several ways the parsing can break, so each method should have a specific exception message associated with it. Right now, all exceptions have the same message.


Discussion / hints:

Fix patch:
diff --git a/dwm1001.py b/dwm1001.py
index bf077fa..ce6b698 100644
--- a/dwm1001.py
+++ b/dwm1001.py
@@ -36,6 +36,10 @@ class ShellCommand(Enum):
     RESET = b"reset"
 
 
+class ParsingError(Exception):
+    pass
+
+
 class UartDwm1001:
     # These delay periods were experimentally determined
     RESET_DELAY_PERIOD = 0.1
@@ -93,8 +97,11 @@ def stop_position_reporting(self) -> None:
     def wait_for_position_report(self) -> Tuple[TagId, TagPosition]:
         report = self.serial_handle.readline().decode().split(",")
 
-        if len(report) != 8 or report[0] != "POS":
-            raise ValueError("Could not parse position report.")
+        if len(report) != 8:
+            raise ParsingError("Position report has unexpected length.")
+
+        if report[0] != "POS":
+            raise ParsingError("Position report has incorrect tag.")
 
         position_data = [float(substr) for substr in report[3:6]]
         position_data.append(int(report[6]))


Test patch:
diff --git a/tests/test_dwm1001.py b/tests/test_dwm1001.py
index 9df6e80..642a589 100644
--- a/tests/test_dwm1001.py
+++ b/tests/test_dwm1001.py
@@ -73,14 +73,14 @@ def test_wait_for_position_report_short(self) -> None:
         listener = dwm1001.Listener(serial_handle)
         listener.start_position_reporting()
 
-        self.assertRaises(ValueError, listener.wait_for_position_report)
+        self.assertRaises(dwm1001.ParsingError, listener.wait_for_position_report)
 
     def test_wait_for_position_report_wrong_header(self) -> None:
         serial_handle = MockSerial(b"BAD,1,TEST1,1.23,4.56,7.89")
         listener = dwm1001.Listener(serial_handle)
         listener.start_position_reporting()
 
-        self.assertRaises(ValueError, listener.wait_for_position_report)
+        self.assertRaises(dwm1001.ParsingError, listener.wait_for_position_report)
 
 
 if __name__ == "__main__":