From 3df79ae42ad7be1b1d51a90081ee0bd1f17465ad Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Fri, 28 Jul 2023 11:46:45 +0200 Subject: [PATCH] Improve exception handling and logging. --- controlpi_plugins/wsserver.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/controlpi_plugins/wsserver.py b/controlpi_plugins/wsserver.py index 2eaf215..31a4ca4 100644 --- a/controlpi_plugins/wsserver.py +++ b/controlpi_plugins/wsserver.py @@ -48,9 +48,13 @@ class Connection: await self._websocket.send(json_message) except ConnectionClosed: pass + except Exception as e: + print(f"WSServer Connection '{self._name}':" + f" Exception while writing to websocket: {e}") async def run(self): """Listen on websocket and relay messages to bus.""" + print(f"WSServer Connection '{self._name}': Connection opened.") await self._bus.send(Message(self._name, {'event': 'connection opened', 'address': self._address, @@ -93,14 +97,18 @@ class Connection: configure_message['mac'] = message['mac'] await self._bus.send(configure_message) except BusException as e: - print(f"Unable to register client '{new_name}'" - f"on bus: {e}") + print(f"WSServer Connection '{self._name}':" + f" Unable to register as client '{new_name}'" + f" on bus: {e}") await self._websocket.close() else: if self._registered: await self._bus.send(Message(self._name, message)) except ConnectionClosed: pass + except Exception as e: + print(f"WSServer Connection '{self._name}':" + f" Exception while reading from websocket: {e}") if self._registered: await self._bus.send(Message(self._name, {'event': 'connection closed'})) @@ -263,8 +271,8 @@ class WSServer(BasePlugin): port=self.conf['port'], process_request=self._process_request, ping_interval=1, ping_timeout=5): - print(f"WSServer '{self.name}'" - f" serving on port {self.conf['port']}.") + print(f"WSServer '{self.name}': Serving on" + f" {self.conf['host']}:{self.conf['port']}.") await stop except OSError: await asyncio.sleep(1) -- 2.34.1