From 803bfa70845c50b10cb09cadefdb6f13433fbc96 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 5 Jan 2020 18:48:48 +0100 Subject: [PATCH] Add proper MQTT error handling --- fbchat/_mqtt.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/fbchat/_mqtt.py b/fbchat/_mqtt.py index 4afb810..2d81131 100644 --- a/fbchat/_mqtt.py +++ b/fbchat/_mqtt.py @@ -53,8 +53,21 @@ class Mqtt: self._configure_connect_options() - # TODO: Handle response code - response_code = mqtt.connect(self._HOST, 443, keepalive=10) + # Attempt to connect + 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 @@ -221,6 +234,9 @@ class Mqtt: return False # Stop listening if rc != paho.mqtt.client.MQTT_ERR_SUCCESS: + err = paho.mqtt.client.error_string(rc) + log.warning("MQTT Error: %s", err) + # Wait before reconnecting self._mqtt._reconnect_wait()