Add proper MQTT error handling

This commit is contained in:
Mads Marquart
2020-01-05 18:48:48 +01:00
parent d1cb866b44
commit 803bfa7084

View File

@@ -53,8 +53,21 @@ class Mqtt:
self._configure_connect_options() self._configure_connect_options()
# TODO: Handle response code # Attempt to connect
response_code = mqtt.connect(self._HOST, 443, keepalive=10) try:
rc = mqtt.connect(self._HOST, 443, keepalive=10)
except (
# Taken from .loop_forever
paho.mqtt.client.socket.error,
OSError,
paho.mqtt.client.WebsocketConnectionError,
) as e:
raise _exception.FBchatException("MQTT connection failed") from e
# Raise error if connecting failed
if rc != paho.mqtt.client.MQTT_ERR_SUCCESS:
err = paho.mqtt.client.error_string(rc)
raise _exception.FBchatException("MQTT connection failed: {}".format(err))
return self return self
@@ -221,6 +234,9 @@ class Mqtt:
return False # Stop listening return False # Stop listening
if rc != paho.mqtt.client.MQTT_ERR_SUCCESS: if rc != paho.mqtt.client.MQTT_ERR_SUCCESS:
err = paho.mqtt.client.error_string(rc)
log.warning("MQTT Error: %s", err)
# Wait before reconnecting # Wait before reconnecting
self._mqtt._reconnect_wait() self._mqtt._reconnect_wait()