-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reduce the use of MMQTTException #240
base: main
Are you sure you want to change the base?
Conversation
use it for protocol/network/system level errors only fixes adafruit#201
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These make sense to me, thanks! Do you see any exception catching in examples here or in https://github.com/adafruit/Adafruit_Learning_System_Guides that might break due to these changes?
@brentru Look good to you? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dhalbert Rather than throwing a generic exception, the library now throws a specific Exception
or Error
. In theory, this looks good to me as it lets a user catch a more specific error rather than a generic.
I do worry about handling these errors, as try/except
in a number of examples may rely on MMQTTException
. It would generate additional support load.
@vladak @dhalbert Do you mind holding off on this until tomorrow PM? I'd like to check through examples that use MiniMQTT on the Learning System Guide repository, Libraries, and in the Adafruit Learning System to see if any Except:
cases would fail due to the change in exception.
@brentru Thanks - I removed my approval to prevent merging until/if you approve. |
@vladak For something like this, where MQTTException is used to catch a generic network error and later reconnect, do you expect it to change? |
@vladak I think the correct change here is Does this look correct to you? |
This is for my notes, to change, and unrelated to the questions above for @vladak. Similar network components that import CircuitPython AWS IoT Library. Though the library is much older and likely incompatible with the current AWS IoT API, we should probably update it for this PR. |
This code: #!/usr/bin/env python3
import json
import socket
import ssl
import sys
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import logging
def main():
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.info("Creating MQTT instance")
broker = "172.40.0.3"
port = 1883
mqtt_client = MQTT.MQTT(
broker=broker,
port=port,
socket_pool=socket,
ssl_context=ssl.create_default_context(),
connect_retries=3,
)
logger.info("Looping before connect()")
try:
mqtt_client.loop()
except MQTT.MMQTTException as e:
logger.error(e.__class__)
sys.exit(1)
logger.info("Connecting to MQTT broker")
mqtt_client.connect()
if __name__ == "__main__":
main() yields the following output on CPython:
If someone wants to catch the logger.info("Looping before connect()")
try:
mqtt_client.loop()
except MQTT.MMQTTStateError as e:
print("wrong state")
sys.exit(1)
except MQTT.MMQTTException as e:
logger.error(e.__class__)
sys.exit(1) which will use the |
Tested on #!/usr/bin/env python3
import ssl
import sys
import adafruit_logging as logging
import socketpool
import wifi
import adafruit_minimqtt.adafruit_minimqtt as MQTT
try:
from secrets import secrets
except ImportError:
print(
"WiFi and Adafruit IO credentials are kept in secrets.py, please add them there!"
)
raise
def main():
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.info("Connecting to wifi")
wifi.radio.connect(secrets["ssid"], secrets["password"], timeout=10)
logger.debug(f"IP: {wifi.radio.ipv4_address}")
pool = socketpool.SocketPool(wifi.radio) # pylint: disable=no-member
logger.info("Creating MQTT instance")
broker = "172.40.0.3"
port = 1883
mqtt_client = MQTT.MQTT(
broker=broker,
port=port,
socket_pool=pool,
ssl_context=ssl.create_default_context(),
)
logger.info("Looping before connect()")
try:
mqtt_client.loop()
except MQTT.MMQTTException as e:
logger.error(e.__class__)
sys.exit(1)
logger.info("Connecting to MQTT broker")
mqtt_client.connect()
if __name__ == "__main__":
main() It produced the following output:
|
This changes some of the exceptions raised. Namely it reduces the use of
MMQTTException
to non-programming errors. For these, I converted the code to useValueError
/NotImplementedError
or the newly introducedMMQTTStateError
(which can be caught viaMMQTTException
).