From c62c9187dc2cc33ea999bfa50d17ab4c9604aec4 Mon Sep 17 00:00:00 2001 From: Maaike Date: Thu, 25 Jul 2024 19:58:35 +0200 Subject: [PATCH] catch host resolution issue --- wis2box-management/wis2box/pubsub/mqtt.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/wis2box-management/wis2box/pubsub/mqtt.py b/wis2box-management/wis2box/pubsub/mqtt.py index 1ce4cf2b7..3d76ea84b 100644 --- a/wis2box-management/wis2box/pubsub/mqtt.py +++ b/wis2box-management/wis2box/pubsub/mqtt.py @@ -68,7 +68,11 @@ def __init__(self, broker: str) -> None: if self.broker_url.scheme == 'mqtts': self.conn.tls_set(tls_version=2) - self.conn.connect(self.broker_url.hostname, self._port) + try: + self.conn.connect(self.broker_url.hostname, self._port) + except Exception as e: + LOGGER.error(f'MQTT Broker connect error: {e}') + self.test_status = e LOGGER.debug('Connection initiated') def pub(self, topic: str, message: str, qos: int = 1) -> bool: @@ -158,6 +162,11 @@ def on_message(client, userdata, message): LOGGER.debug(f'Test: Received message {message.payload.decode()}') self.test_status = 'success' + if self.test_status != 'unknown': + msg = f'Test: failed to connect to MQTT-broker: {self.test_status}' # noqa + LOGGER.error(msg) + return self.test_status == 'success' + self.conn.on_connect = on_connect self.conn.on_message = on_message