浏览代码

Cleanup handling of MQTT errors

Always send a `Disconnect` event and include the full MQTT error code
in any exceptions so we have them in proxy assignment logs.
Nick Barrett 2 年之前
父节点
当前提交
cfbe1062a7
共有 1 个文件被更改,包括 7 次插入5 次删除
  1. 7 5
      mauigpapi/mqtt/conn.py

+ 7 - 5
mauigpapi/mqtt/conn.py

@@ -599,21 +599,23 @@ class AndroidMQTT:
                     # If known/expected error
                     if rc == pmc.MQTT_ERR_CONN_LOST:
                         await self._dispatch(Disconnect(reason="Connection lost, retrying"))
-                        raise MQTTNotConnected
+                        raise MQTTNotConnected("MQTT_ERR_CONN_LOST")
                     elif rc == pmc.MQTT_ERR_NOMEM:
                         # This error is wrongly classified
                         # See https://github.com/eclipse/paho.mqtt.python/issues/340
                         await self._dispatch(Disconnect(reason="Connection lost, retrying"))
-                        raise MQTTNotConnected
+                        raise MQTTNotConnected("MQTT_ERR_NOMEM")
                     elif rc == pmc.MQTT_ERR_CONN_REFUSED:
-                        raise MQTTNotLoggedIn("MQTT connection refused")
+                        await self._dispatch(Disconnect(reason="Connection refused, retrying"))
+                        raise MQTTNotLoggedIn("MQTT_ERR_CONN_REFUSED")
                     elif rc == pmc.MQTT_ERR_NO_CONN:
-                        raise MQTTNotConnected("Connection failed")
+                        await self._dispatch(Disconnect(reason="Connection dropped, retrying"))
+                        raise MQTTNotConnected("MQTT_ERR_NO_CONN")
                     else:
                         err = pmc.error_string(rc)
                         self.log.error("MQTT Error: %s", err)
                         await self._dispatch(Disconnect(reason=f"MQTT Error: {err}, retrying"))
-                        raise MQTTNotConnected
+                        raise MQTTNotConnected(err)
 
         await proxy_with_retry(
             "mqtt.listen",