paho.mqtt.python icon indicating copy to clipboard operation
paho.mqtt.python copied to clipboard

Examples ignore errors, because Client does not always raise exceptions

Open theamk opened this issue 5 years ago • 0 comments

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)

theamk avatar Dec 09 '19 17:12 theamk