Skip to content

Commit 3606085

Browse files
committed
Add exception handling for RobotRumble file open
1 parent 02b6cce commit 3606085

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

codeclash/games/robotrumble/robotrumble.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ def execute_round(self, agents: list[Player]):
6969
future.result()
7070

7171
def _get_winner_txt(self, output_file: str, agents: list[Player]) -> str:
72-
with open(output_file) as f:
73-
lines = f.read().strip().split("\n")
72+
try:
73+
with open(output_file) as f:
74+
lines = f.read().strip().split("\n")
75+
except Exception as e:
76+
self.logger.warning(f"Failed to read output from {output_file}: {e}")
77+
return RESULT_TIE # TODO: should this be a tie?
7478

7579
# Get the last 2 lines which contain the game result (same as original)
7680
relevant_lines = lines[-2:] if len(lines) >= 2 else lines
@@ -85,8 +89,12 @@ def _get_winner_txt(self, output_file: str, agents: list[Player]) -> str:
8589
return RESULT_TIE
8690

8791
def _get_winner_json(self, output_file: str, agents: list[Player]) -> str:
88-
with open(output_file) as f:
89-
data = json.load(f)
92+
try:
93+
with open(output_file) as f:
94+
data = json.load(f)
95+
except json.JSONDecodeError:
96+
self.logger.warning(f"Failed to parse JSON output from {output_file}")
97+
return RESULT_TIE # TODO: should this be a tie?
9098
if "winner" in data:
9199
if data["winner"] == "Blue":
92100
return agents[0].name

0 commit comments

Comments
 (0)