Skip to content

Commit a294aec

Browse files
author
brentru
committed
break out errors into explicit types
1 parent f056b83 commit a294aec

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

Adafruit_IO/errors.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121

2222
import json
2323

24+
# MQTT RC Error Types
25+
MQTT_ERRORS = [ 'Connection successful',
26+
'Incorrect protocol version',
27+
'Invalid Client ID',
28+
'Server unavailable ',
29+
'Bad username or password',
30+
'Not authorized' ]
31+
2432
class AdafruitIOError(Exception):
2533
"""Base class for all Adafruit IO request failures."""
2634
pass
@@ -49,3 +57,11 @@ def __init__(self):
4957
super(ThrottlingError, self).__init__("Exceeded the limit of Adafruit IO " \
5058
"requests in a short period of time. Please reduce the rate of requests " \
5159
"and try again later.")
60+
61+
class MQTTError(Exception):
62+
"""Handles connection attempt failed errors.
63+
"""
64+
def __init__(self, response):
65+
error = MQTT_ERRORS[response]
66+
super(MQTTError, self).__init__(error)
67+
pass

Adafruit_IO/mqtt_client.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
import logging
2222

2323
import paho.mqtt.client as mqtt
24-
24+
import sys
25+
from .errors import MQTTError, RequestError
2526

2627
# How long to wait before sending a keep alive (paho-mqtt configuration).
2728
KEEP_ALIVE_SEC = 60 # One minute
@@ -62,11 +63,12 @@ def _mqtt_connect(self, client, userdata, flags, rc):
6263
# Check if the result code is success (0) or some error (non-zero) and
6364
# raise an exception if failed.
6465
if rc == 0:
66+
#raise RequestError(rc)
6567
self._connected = True
68+
print('Connected to Adafruit IO!')
6669
else:
67-
# TODO: Make explicit exception classes for these failures:
68-
# 0: Connection successful 1: Connection refused - incorrect protocol version 2: Connection refused - invalid client identifier 3: Connection refused - server unavailable 4: Connection refused - bad username or password 5: Connection refused - not authorised 6-255: Currently unused.
69-
raise RuntimeError('Error connecting to Adafruit IO with rc: {0}'.format(rc))
70+
# handle RC errors within `errors.py`'s MQTTError class
71+
raise MQTTError(rc)
7072
# Call the on_connect callback if available.
7173
if self.on_connect is not None:
7274
self.on_connect(self)
@@ -78,7 +80,8 @@ def _mqtt_disconnect(self, client, userdata, rc):
7880
# log the RC as an error. Continue on to call any disconnect handler
7981
# so clients can potentially recover gracefully.
8082
if rc != 0:
81-
logger.debug('Unexpected disconnect with rc: {0}'.format(rc))
83+
raise MQTTError(rc)
84+
print('Disconnected from Adafruit IO!')
8285
# Call the on_disconnect callback if available.
8386
if self.on_disconnect is not None:
8487
self.on_disconnect(self)

0 commit comments

Comments
 (0)