Skip to content

Commit

Permalink
catch host resolution issue
Browse files Browse the repository at this point in the history
  • Loading branch information
maaikelimper committed Jul 25, 2024
1 parent c6da9bd commit c62c918
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion wis2box-management/wis2box/pubsub/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit c62c918

Please sign in to comment.