From c4b704721ecc005b6d3d0b9158dd9e512f5f41e0 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 1 Jan 2026 03:18:19 +0100 Subject: [PATCH] handle exceptions --- rpi/controllers/GameController.py | 11 ++++--- rpi/models/exceptions/ServiceException.py | 4 +++ rpi/models/exceptions/__init__.py | 0 rpi/services/game_service.py | 36 +++++++++++++++-------- 4 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 rpi/models/exceptions/ServiceException.py create mode 100644 rpi/models/exceptions/__init__.py diff --git a/rpi/controllers/GameController.py b/rpi/controllers/GameController.py index 1d9f3829..b7bb8336 100644 --- a/rpi/controllers/GameController.py +++ b/rpi/controllers/GameController.py @@ -4,6 +4,7 @@ import threading import requests from flask import jsonify, request +from models.exceptions.ServiceException import ServiceException from services.game_service import GameService class GameController: @@ -16,7 +17,6 @@ class GameController: self._game_service = GameService() self._register_routes(app) self._auth_token = "0eed89e8-7625-4f8d-bf2a-0872aede0efb" - self._has_started = False def _register_routes(self, app): app.add_url_rule("/command/party/start", view_func=self.start_party, methods=['POST']) @@ -36,15 +36,15 @@ class GameController: self._game_service.start(white_name, black_name, time_control, increment) self._has_started = True return jsonify({"status": "ok", "message": "Game started"}), 200 + + except ServiceException as ex: + return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 400 except Exception as ex: print(ex) return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 500 def make_move(self): try: - if not self._has_started: - return jsonify({"status": "error", "message": "Game hasn't started yet"}), 400 - auth_token = request.headers.get("Authorization") if auth_token != "Bearer " + self._auth_token: return jsonify({"status": "error", "message": "Invalid authorization token"}), 401 @@ -55,6 +55,9 @@ class GameController: ).start() return jsonify({"status": "ok"}), 200 + + except ServiceException as ex: + return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 400 except Exception as ex: print(ex) return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 500 diff --git a/rpi/models/exceptions/ServiceException.py b/rpi/models/exceptions/ServiceException.py new file mode 100644 index 00000000..55ce165f --- /dev/null +++ b/rpi/models/exceptions/ServiceException.py @@ -0,0 +1,4 @@ +class ServiceException(Exception): + def __init__(self, message): + self.message = message + super().__init__(self.message) \ No newline at end of file diff --git a/rpi/models/exceptions/__init__.py b/rpi/models/exceptions/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/rpi/services/game_service.py b/rpi/services/game_service.py index 47d7642d..bb85a991 100644 --- a/rpi/services/game_service.py +++ b/rpi/services/game_service.py @@ -1,28 +1,40 @@ +from models.exceptions.ServiceException import ServiceException from services.clock_service import ClockService from services.detection_service import DetectionService class GameService: - detection_service : DetectionService - clock_service : ClockService + _detection_service : DetectionService + _clock_service : ClockService + _has_started : bool def __init__(self): - self.detection_service = DetectionService() - self.clock_service = ClockService() + self._detection_service = DetectionService() + self._clock_service = ClockService() + self._has_started = False def start(self, white_name, back_name, time_control : int, increment : int ) -> None: - self.clock_service.start(time_control, increment) - self.clock_service.set_on_terminated(self.stop) + if self._has_started : + raise ServiceException("Game has already started.") + self._clock_service.start(time_control, increment) + self._clock_service.set_on_terminated(self.stop) + self._has_started = True def stop(self): - self.clock_service.stop() - self.detection_service.stop() + self._clock_service.stop() + self._detection_service.stop() + self._has_started = False def make_move(self) -> tuple[bytes, str] | None: try : - self.clock_service.switch() - return self.detection_service.analyze_single_frame() - except Exception as e: - print(e) + if not self._has_started : + raise ServiceException("Game hasn't started yet.") + self._clock_service.switch() + return self._detection_service.analyze_single_frame() + except ServiceException as se: + print(se) + return None + except Exception as ex: + print(ex) return None \ No newline at end of file