95
95
96
96
97
97
class MMQTTException (Exception ):
98
- """MiniMQTT Exception class."""
98
+ """
99
+ MiniMQTT Exception class.
100
+
101
+ Raised for various mostly protocol or network/system level errors.
102
+ In general, the robust way to recover is to call reconnect().
103
+ """
99
104
100
105
def __init__ (self , error , code = None ):
101
106
super ().__init__ (error , code )
102
107
self .code = code
103
108
104
109
110
+ class MMQTTStateError (MMQTTException ):
111
+ """
112
+ MiniMQTT invalid state error.
113
+
114
+ Raised e.g. if a function is called in unexpected state.
115
+ """
116
+
117
+
105
118
class NullLogger :
106
119
"""Fake logger class that does not do anything"""
107
120
@@ -165,7 +178,7 @@ def __init__( # noqa: PLR0915, PLR0913, Too many statements, Too many arguments
165
178
self ._use_binary_mode = use_binary_mode
166
179
167
180
if recv_timeout <= socket_timeout :
168
- raise MMQTTException ("recv_timeout must be strictly greater than socket_timeout" )
181
+ raise ValueError ("recv_timeout must be strictly greater than socket_timeout" )
169
182
self ._socket_timeout = socket_timeout
170
183
self ._recv_timeout = recv_timeout
171
184
@@ -183,7 +196,7 @@ def __init__( # noqa: PLR0915, PLR0913, Too many statements, Too many arguments
183
196
self ._reconnect_timeout = float (0 )
184
197
self ._reconnect_maximum_backoff = 32
185
198
if connect_retries <= 0 :
186
- raise MMQTTException ("connect_retries must be positive" )
199
+ raise ValueError ("connect_retries must be positive" )
187
200
self ._reconnect_attempts_max = connect_retries
188
201
189
202
self .broker = broker
@@ -192,7 +205,7 @@ def __init__( # noqa: PLR0915, PLR0913, Too many statements, Too many arguments
192
205
if (
193
206
self ._password and len (password .encode ("utf-8" )) > MQTT_TOPIC_LENGTH_LIMIT
194
207
): # [MQTT-3.1.3.5]
195
- raise MMQTTException ("Password length is too large." )
208
+ raise ValueError ("Password length is too large." )
196
209
197
210
# The connection will be insecure unless is_ssl is set to True.
198
211
# If the port is not specified, the security will be set based on the is_ssl parameter.
@@ -288,28 +301,27 @@ def will_set(
288
301
"""
289
302
self .logger .debug ("Setting last will properties" )
290
303
if self ._is_connected :
291
- raise MMQTTException ("Last Will should only be called before connect()." )
304
+ raise MMQTTStateError ("Last Will should only be called before connect()." )
292
305
293
306
# check topic/msg/qos kwargs
294
307
self ._valid_topic (topic )
295
308
if "+" in topic or "#" in topic :
296
- raise MMQTTException ("Publish topic can not contain wildcards." )
309
+ raise ValueError ("Publish topic can not contain wildcards." )
297
310
298
311
if msg is None :
299
- raise MMQTTException ("Message can not be None." )
312
+ raise ValueError ("Message can not be None." )
300
313
if isinstance (msg , (int , float )):
301
314
msg = str (msg ).encode ("ascii" )
302
315
elif isinstance (msg , str ):
303
316
msg = str (msg ).encode ("utf-8" )
304
317
elif isinstance (msg , bytes ):
305
318
pass
306
319
else :
307
- raise MMQTTException ("Invalid message data type." )
320
+ raise ValueError ("Invalid message data type." )
308
321
if len (msg ) > MQTT_MSG_MAX_SZ :
309
- raise MMQTTException (f"Message size larger than { MQTT_MSG_MAX_SZ } bytes." )
322
+ raise ValueError (f"Message size larger than { MQTT_MSG_MAX_SZ } bytes." )
310
323
311
324
self ._valid_qos (qos )
312
- assert 0 <= qos <= 1 , "Quality of Service Level 2 is unsupported by this library."
313
325
314
326
# fixed header. [3.3.1.2], [3.3.1.3]
315
327
pub_hdr_fixed = bytearray ([MQTT_PUBLISH | retain | qos << 1 ])
@@ -392,7 +404,7 @@ def username_pw_set(self, username: str, password: Optional[str] = None) -> None
392
404
393
405
"""
394
406
if self ._is_connected :
395
- raise MMQTTException ("This method must be called before connect()." )
407
+ raise MMQTTStateError ("This method must be called before connect()." )
396
408
self ._username = username
397
409
if password is not None :
398
410
self ._password = password
@@ -672,21 +684,22 @@ def publish( # noqa: PLR0912, Too many branches
672
684
self ._connected ()
673
685
self ._valid_topic (topic )
674
686
if "+" in topic or "#" in topic :
675
- raise MMQTTException ("Publish topic can not contain wildcards." )
687
+ raise ValueError ("Publish topic can not contain wildcards." )
676
688
# check msg/qos kwargs
677
689
if msg is None :
678
- raise MMQTTException ("Message can not be None." )
690
+ raise ValueError ("Message can not be None." )
679
691
if isinstance (msg , (int , float )):
680
692
msg = str (msg ).encode ("ascii" )
681
693
elif isinstance (msg , str ):
682
694
msg = str (msg ).encode ("utf-8" )
683
695
elif isinstance (msg , bytes ):
684
696
pass
685
697
else :
686
- raise MMQTTException ("Invalid message data type." )
698
+ raise ValueError ("Invalid message data type." )
687
699
if len (msg ) > MQTT_MSG_MAX_SZ :
688
- raise MMQTTException (f"Message size larger than { MQTT_MSG_MAX_SZ } bytes." )
689
- assert 0 <= qos <= 1 , "Quality of Service Level 2 is unsupported by this library."
700
+ raise ValueError (f"Message size larger than { MQTT_MSG_MAX_SZ } bytes." )
701
+
702
+ self ._valid_qos (qos )
690
703
691
704
# fixed header. [3.3.1.2], [3.3.1.3]
692
705
pub_hdr_fixed = bytearray ([MQTT_PUBLISH | retain | qos << 1 ])
@@ -851,7 +864,7 @@ def unsubscribe( # noqa: PLR0912, Too many branches
851
864
topics .append (t )
852
865
for t in topics :
853
866
if t not in self ._subscribed_topics :
854
- raise MMQTTException ("Topic must be subscribed to before attempting unsubscribe." )
867
+ raise MMQTTStateError ("Topic must be subscribed to before attempting unsubscribe." )
855
868
# Assemble packet
856
869
self .logger .debug ("Sending UNSUBSCRIBE to broker..." )
857
870
fixed_header = bytearray ([MQTT_UNSUB ])
@@ -961,7 +974,7 @@ def loop(self, timeout: float = 1.0) -> Optional[list[int]]:
961
974
962
975
"""
963
976
if timeout < self ._socket_timeout :
964
- raise MMQTTException (
977
+ raise ValueError (
965
978
f"loop timeout ({ timeout } ) must be >= "
966
979
+ f"socket timeout ({ self ._socket_timeout } ))"
967
980
)
@@ -1155,13 +1168,13 @@ def _valid_topic(topic: str) -> None:
1155
1168
1156
1169
"""
1157
1170
if topic is None :
1158
- raise MMQTTException ("Topic may not be NoneType" )
1171
+ raise ValueError ("Topic may not be NoneType" )
1159
1172
# [MQTT-4.7.3-1]
1160
1173
if not topic :
1161
- raise MMQTTException ("Topic may not be empty." )
1174
+ raise ValueError ("Topic may not be empty." )
1162
1175
# [MQTT-4.7.3-3]
1163
1176
if len (topic .encode ("utf-8" )) > MQTT_TOPIC_LENGTH_LIMIT :
1164
- raise MMQTTException ( "Topic length is too large. " )
1177
+ raise ValueError ( f"Encoded topic length is larger than { MQTT_TOPIC_LENGTH_LIMIT } " )
1165
1178
1166
1179
@staticmethod
1167
1180
def _valid_qos (qos_level : int ) -> None :
@@ -1172,16 +1185,16 @@ def _valid_qos(qos_level: int) -> None:
1172
1185
"""
1173
1186
if isinstance (qos_level , int ):
1174
1187
if qos_level < 0 or qos_level > 2 :
1175
- raise MMQTTException ("QoS must be between 1 and 2." )
1188
+ raise NotImplementedError ("QoS must be between 1 and 2." )
1176
1189
else :
1177
- raise MMQTTException ("QoS must be an integer." )
1190
+ raise ValueError ("QoS must be an integer." )
1178
1191
1179
1192
def _connected (self ) -> None :
1180
1193
"""Returns MQTT client session status as True if connected, raises
1181
- a `MMQTTException ` if `False`.
1194
+ a `MMQTTStateError exception ` if `False`.
1182
1195
"""
1183
1196
if not self .is_connected ():
1184
- raise MMQTTException ("MiniMQTT is not connected" )
1197
+ raise MMQTTStateError ("MiniMQTT is not connected" )
1185
1198
1186
1199
def is_connected (self ) -> bool :
1187
1200
"""Returns MQTT client session status as True if connected, False
0 commit comments