2

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
Jason Small
  • 1,064
  • 1
  • 13
  • 29

1 Answers1

2

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.

meuh
  • 11,500
  • 2
  • 29
  • 45
  • 1
    Thanks that worked! Also adding "from adafruit_minimqtt.adafruit_minimqtt import MMQTTException" to the top of the file worked as well. – Jason Small Jan 29 '21 at 16:45