paho.mqtt.python
paho.mqtt.python copied to clipboard
Examples ignore errors, because Client does not always raise exceptions
The current API uses error return codes (see #142), which needs to be checked for each relevant call. However, documentation does not mention it, and the examples do not so it either.
For example. https://github.com/eclipse/paho.mqtt.python/blob/master/examples/client_sub.py says:
mqttc.connect("mqtt.eclipse.org", 1883, 60)
mqttc.subscribe("$SYS/#", 0)
The correct code would be:
result = mqttc.connect("mqtt.eclipse.org", 1883, 60)
if result != mqtt.MQTT_ERR_SUCCESS:
raise Exception("mqtt connect failed: " + mqtt.error_string(result))
result, mid = mqttc.subscribe("$SYS/#", 0)
if result != mqtt.MQTT_ERR_SUCCESS:
raise Exception("mqtt subscribe failed: " + mqtt.error_string(result))
I think that either:
- Every example should be fixed to properly error-check
- .. or an API should be changed to always raise on error.
(I personally prefer latter solution, because that's how every other python library works)