Skip to content

Commit a693add

Browse files
authored
Merge pull request #208 from robotpy/better-hal-error
Provide better error messages if a user reuses a HAL object
2 parents d6c7a0e + 2d5ea15 commit a693add

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

pyfrc/test_support/controller.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ def run_robot(self):
7878
self._robot_finished = True
7979
robot.endCompetition()
8080

81+
if isinstance(self._reraise.exception, RuntimeError):
82+
if str(self._reraise.exception).startswith(
83+
"HAL: A handle parameter was passed incorrectly"
84+
):
85+
msg = (
86+
"Do not reuse HAL objects in tests! This error may occur if you"
87+
" stored a motor/sensor as a global or as a class variable"
88+
" outside of a method."
89+
)
90+
if hasattr(Exception, "add_note"):
91+
self._reraise.exception.add_note(f"*** {msg}")
92+
else:
93+
e = self._reraise.exception
94+
self._reraise.reset()
95+
raise RuntimeError(msg) from e
96+
8197
# Increment time by 1 second to ensure that any notifiers fire
8298
stepTimingAsync(1.0)
8399

@@ -106,7 +122,7 @@ def step_timing(
106122
seconds: float,
107123
autonomous: bool,
108124
enabled: bool,
109-
assert_alive: bool = True
125+
assert_alive: bool = True,
110126
) -> float:
111127
"""
112128
This utility will increment simulated time, while pretending that

0 commit comments

Comments
 (0)