Search code examples
pythonmicropythonadafruitadafruit-circuitpython

Circuit Python MQTT Exception Block


I'm using the Adafruit Circuit Python MQTT library and am trying to catch the errors being generated.

   while True:
    try:
        # Poll the message queue
        mqtt_client.loop()
    except (ValueError, RuntimeError, MMQTTException) as e:
        print("Failed to get data, retrying\n", e)
      
        mqtt_client.reconnect()
        # continue
    time.sleep(1)

But this generates the following error:

NameError: name 'MMQTTException' is not defined

Any ideas how I should properly catch this error?

The library has the following error class. I'm guessing it needs to be exposed somehow?

class MMQTTException(Exception):
    """MiniMQTT Exception class."""

    # pylint: disable=unnecessary-pass
    # pass

Solution

  • If you did something like

    import adafruit_minimqtt.adafruit_minimqtt as MQTT
    

    in order to be able to use mqtt_client = MQTT.MQTT(...), then you need to refer to this other class similarly, as MQTT.MMQTTException.