handle exceptions
This commit is contained in:
@@ -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
|
||||
|
||||
4
rpi/models/exceptions/ServiceException.py
Normal file
4
rpi/models/exceptions/ServiceException.py
Normal file
@@ -0,0 +1,4 @@
|
||||
class ServiceException(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
super().__init__(self.message)
|
||||
0
rpi/models/exceptions/__init__.py
Normal file
0
rpi/models/exceptions/__init__.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user