diff --git a/obsws_python/baseclient.py b/obsws_python/baseclient.py index 17b85a0..4f47d4b 100644 --- a/obsws_python/baseclient.py +++ b/obsws_python/baseclient.py @@ -7,15 +7,22 @@ from random import randint from typing import Optional import websocket -from websocket._exceptions import WebSocketTimeoutException -from .error import OBSSDKError +from websocket import WebSocketTimeoutException + +from .error import OBSSDKError, OBSSDKTimeoutError class ObsClient: logger = logging.getLogger("baseclient.obsclient") def __init__(self, **kwargs): - defaultkwargs = {"host": "localhost", "port": 4455, "password": "", "subs": 0, "timeout":None} + defaultkwargs = { + "host": "localhost", + "port": 4455, + "password": "", + "subs": 0, + "timeout": None, + } if not any(key in kwargs for key in ("host", "port", "password")): kwargs |= self._conn_from_toml() kwargs = defaultkwargs | kwargs @@ -23,14 +30,21 @@ class ObsClient: setattr(self, attr, val) self.logger.info( - "Connecting with parameters: host='{host}' port={port} password='{password}' subs={subs}".format( + "Connecting with parameters: host='{host}' port={port} password='{password}' subs={subs} timeout={timeout}".format( **self.__dict__ ) ) - self.ws = websocket.WebSocket() - self.ws.connect(f"ws://{self.host}:{self.port}", timeout=self.timeout) - self.server_hello = json.loads(self.ws.recv()) + try: + self.ws = websocket.WebSocket() + self.ws.connect(f"ws://{self.host}:{self.port}", timeout=self.timeout) + self.server_hello = json.loads(self.ws.recv()) + except ValueError as e: + self.logger.error(f"{type(e).__name__}: {e}") + raise + except (ConnectionRefusedError, WebSocketTimeoutException) as e: + self.logger.exception(f"{type(e).__name__}: {e}") + raise def _conn_from_toml(self) -> dict: try: @@ -108,7 +122,8 @@ class ObsClient: try: self.ws.send(json.dumps(payload)) response = json.loads(self.ws.recv()) - except WebSocketTimeoutException : - raise OBSSDKError("Timeout while trying to send the request") + except WebSocketTimeoutException as e: + self.logger.exception(f"{type(e).__name__}: {e}") + raise OBSSDKTimeoutError("Timeout while trying to send the request") from e self.logger.debug(f"Response received {response}") return response["d"] diff --git a/obsws_python/events.py b/obsws_python/events.py index 9a942e6..a358fe7 100644 --- a/obsws_python/events.py +++ b/obsws_python/events.py @@ -3,8 +3,11 @@ import logging import time from threading import Thread +from websocket import WebSocketTimeoutException + from .baseclient import ObsClient from .callback import Callback +from .error import OBSSDKTimeoutError from .subs import Subs """ @@ -30,7 +33,7 @@ class EventClient: def __repr__(self): return type( self - ).__name__ + "(host='{host}', port={port}, password='{password}', subs={subs})".format( + ).__name__ + "(host='{host}', port={port}, password='{password}', subs={subs}, timeout={timeout})".format( **self.base_client.__dict__, ) @@ -49,7 +52,11 @@ class EventClient: """ self.running = True while self.running: - event = json.loads(self.base_client.ws.recv()) + try: + event = json.loads(self.base_client.ws.recv()) + except WebSocketTimeoutException as e: + self.logger.exception(f"{type(e).__name__}: {e}") + raise OBSSDKTimeoutError("Timeout while waiting for event") from e self.logger.debug(f"Event received {event}") type_, data = ( event["d"].get("eventType"), diff --git a/obsws_python/reqs.py b/obsws_python/reqs.py index 9650e95..fb7a809 100644 --- a/obsws_python/reqs.py +++ b/obsws_python/reqs.py @@ -28,7 +28,7 @@ class ReqClient: def __repr__(self): return type( self - ).__name__ + "(host='{host}', port={port}, password='{password}')".format( + ).__name__ + "(host='{host}', port={port}, password='{password}', timeout={timeout})".format( **self.base_client.__dict__, )