handle exceptions
This commit is contained in:
@@ -4,6 +4,7 @@ import threading
|
|||||||
import requests
|
import requests
|
||||||
from flask import jsonify, request
|
from flask import jsonify, request
|
||||||
|
|
||||||
|
from models.exceptions.ServiceException import ServiceException
|
||||||
from services.game_service import GameService
|
from services.game_service import GameService
|
||||||
|
|
||||||
class GameController:
|
class GameController:
|
||||||
@@ -16,7 +17,6 @@ class GameController:
|
|||||||
self._game_service = GameService()
|
self._game_service = GameService()
|
||||||
self._register_routes(app)
|
self._register_routes(app)
|
||||||
self._auth_token = "0eed89e8-7625-4f8d-bf2a-0872aede0efb"
|
self._auth_token = "0eed89e8-7625-4f8d-bf2a-0872aede0efb"
|
||||||
self._has_started = False
|
|
||||||
|
|
||||||
def _register_routes(self, app):
|
def _register_routes(self, app):
|
||||||
app.add_url_rule("/command/party/start", view_func=self.start_party, methods=['POST'])
|
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._game_service.start(white_name, black_name, time_control, increment)
|
||||||
self._has_started = True
|
self._has_started = True
|
||||||
return jsonify({"status": "ok", "message": "Game started"}), 200
|
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:
|
except Exception as ex:
|
||||||
print(ex)
|
print(ex)
|
||||||
return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 500
|
return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 500
|
||||||
|
|
||||||
def make_move(self):
|
def make_move(self):
|
||||||
try:
|
try:
|
||||||
if not self._has_started:
|
|
||||||
return jsonify({"status": "error", "message": "Game hasn't started yet"}), 400
|
|
||||||
|
|
||||||
auth_token = request.headers.get("Authorization")
|
auth_token = request.headers.get("Authorization")
|
||||||
if auth_token != "Bearer " + self._auth_token:
|
if auth_token != "Bearer " + self._auth_token:
|
||||||
return jsonify({"status": "error", "message": "Invalid authorization token"}), 401
|
return jsonify({"status": "error", "message": "Invalid authorization token"}), 401
|
||||||
@@ -55,6 +55,9 @@ class GameController:
|
|||||||
).start()
|
).start()
|
||||||
|
|
||||||
return jsonify({"status": "ok"}), 200
|
return jsonify({"status": "ok"}), 200
|
||||||
|
|
||||||
|
except ServiceException as ex:
|
||||||
|
return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 400
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print(ex)
|
print(ex)
|
||||||
return jsonify({"status": "error", "message": f"An error occurred : {ex}"}), 500
|
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.clock_service import ClockService
|
||||||
from services.detection_service import DetectionService
|
from services.detection_service import DetectionService
|
||||||
|
|
||||||
|
|
||||||
class GameService:
|
class GameService:
|
||||||
|
|
||||||
detection_service : DetectionService
|
_detection_service : DetectionService
|
||||||
clock_service : ClockService
|
_clock_service : ClockService
|
||||||
|
_has_started : bool
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.detection_service = DetectionService()
|
self._detection_service = DetectionService()
|
||||||
self.clock_service = ClockService()
|
self._clock_service = ClockService()
|
||||||
|
self._has_started = False
|
||||||
|
|
||||||
def start(self, white_name, back_name, time_control : int, increment : int ) -> None:
|
def start(self, white_name, back_name, time_control : int, increment : int ) -> None:
|
||||||
self.clock_service.start(time_control, increment)
|
if self._has_started :
|
||||||
self.clock_service.set_on_terminated(self.stop)
|
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):
|
def stop(self):
|
||||||
self.clock_service.stop()
|
self._clock_service.stop()
|
||||||
self.detection_service.stop()
|
self._detection_service.stop()
|
||||||
|
self._has_started = False
|
||||||
|
|
||||||
def make_move(self) -> tuple[bytes, str] | None:
|
def make_move(self) -> tuple[bytes, str] | None:
|
||||||
try :
|
try :
|
||||||
self.clock_service.switch()
|
if not self._has_started :
|
||||||
return self.detection_service.analyze_single_frame()
|
raise ServiceException("Game hasn't started yet.")
|
||||||
except Exception as e:
|
self._clock_service.switch()
|
||||||
print(e)
|
return self._detection_service.analyze_single_frame()
|
||||||
|
except ServiceException as se:
|
||||||
|
print(se)
|
||||||
|
return None
|
||||||
|
except Exception as ex:
|
||||||
|
print(ex)
|
||||||
return None
|
return None
|
||||||
Reference in New Issue
Block a user