From: Benjamin Braatz Date: Fri, 28 Jul 2023 09:47:17 +0000 (+0200) Subject: Improve exception handling and logging. X-Git-Tag: v0.3.2~1 X-Git-Url: http://git.graph-it.com/?a=commitdiff_plain;h=9174722eba32b2d29156719eeb6f5d0c33e1cadc;p=graphit%2Fcontrolpi-wsclient.git Improve exception handling and logging. --- diff --git a/controlpi_plugins/wsclient.py b/controlpi_plugins/wsclient.py index 9ace98f..fd81680 100644 --- a/controlpi_plugins/wsclient.py +++ b/controlpi_plugins/wsclient.py @@ -1,11 +1,10 @@ """Provide a client ControlPi plugin WSClient for websockets.""" import asyncio -import asyncio.exceptions import fcntl import json import socket import struct -from websockets.exceptions import ConnectionClosed, InvalidMessage +from websockets.exceptions import ConnectionClosed from websockets.client import connect, WebSocketClientProtocol from controlpi import BasePlugin, Message, MessageTemplate, BusException @@ -197,6 +196,8 @@ class WSClient(BasePlugin): """Connect to wsserver and process messages from it.""" async for websocket in connect(self.conf['url'], ping_interval=1, ping_timeout=5): + print(f"WSClient '{self.name}': Connection opened" + f" to {self.conf['url']}.") await self.bus.send(Message(self.name, {'event': 'connection opened'})) @@ -229,9 +230,8 @@ class WSClient(BasePlugin): await self.bus.send(Message(self.name, {'event': 'connection closed'})) - except (OSError, InvalidMessage, - asyncio.exceptions.TimeoutError) as e: - print(f"WSClient to {self.conf['url']} connection: {e}") + except Exception as e: + print(f"WSClient '{self.name}': Exception in connection: {e}") await asyncio.sleep(1) async def _send(self, json_message: str) -> None: @@ -242,4 +242,4 @@ class WSClient(BasePlugin): try: await self.bus.send(Message(self.name, translated_message)) except BusException as e: - print(f"WSClient to {self.conf['url']} local bus: {e}") + print(f"WSClient '{self.name}': Exception on local bus: {e}")