@@ -436,6 +436,9 @@ def on_connect(client, userdata, flags, rc, properties=None):
436
436
"userdata" is user data of any type and can be set when creating a new client
437
437
instance or with user_data_set(userdata).
438
438
439
+ If you wish to suppress exceptions within a callback, you should set
440
+ `client.suppress_exceptions = True`
441
+
439
442
The callbacks:
440
443
441
444
on_connect(client, userdata, flags, rc, properties=None): called when the broker responds to our connection
@@ -651,6 +654,7 @@ def __init__(self, client_id="", clean_session=None, userdata=None,
651
654
self ._websocket_extra_headers = None
652
655
# for clean_start == MQTT_CLEAN_START_FIRST_ONLY
653
656
self ._mqttv5_first_connect = True
657
+ self .suppress_exceptions = False # For callbacks
654
658
655
659
def __del__ (self ):
656
660
self ._reset_sockets ()
@@ -2068,6 +2072,8 @@ def _call_socket_open(self):
2068
2072
except Exception as err :
2069
2073
self ._easy_log (
2070
2074
MQTT_LOG_ERR , 'Caught exception in on_socket_open: %s' , err )
2075
+ if not self .suppress_exceptions :
2076
+ raise
2071
2077
2072
2078
@property
2073
2079
def on_socket_close (self ):
@@ -2100,6 +2106,8 @@ def _call_socket_close(self, sock):
2100
2106
except Exception as err :
2101
2107
self ._easy_log (
2102
2108
MQTT_LOG_ERR , 'Caught exception in on_socket_close: %s' , err )
2109
+ if not self .suppress_exceptions :
2110
+ raise
2103
2111
2104
2112
@property
2105
2113
def on_socket_register_write (self ):
@@ -2135,6 +2143,8 @@ def _call_socket_register_write(self):
2135
2143
except Exception as err :
2136
2144
self ._easy_log (
2137
2145
MQTT_LOG_ERR , 'Caught exception in on_socket_register_write: %s' , err )
2146
+ if not self .suppress_exceptions :
2147
+ raise
2138
2148
2139
2149
@property
2140
2150
def on_socket_unregister_write (self ):
@@ -2171,6 +2181,8 @@ def _call_socket_unregister_write(self, sock=None):
2171
2181
except Exception as err :
2172
2182
self ._easy_log (
2173
2183
MQTT_LOG_ERR , 'Caught exception in on_socket_unregister_write: %s' , err )
2184
+ if not self .suppress_exceptions :
2185
+ raise
2174
2186
2175
2187
def message_callback_add (self , sub , callback ):
2176
2188
"""Register a message callback for a specific topic.
@@ -2348,6 +2360,8 @@ def _packet_write(self):
2348
2360
except Exception as err :
2349
2361
self ._easy_log (
2350
2362
MQTT_LOG_ERR , 'Caught exception in on_publish: %s' , err )
2363
+ if not self .suppress_exceptions :
2364
+ raise
2351
2365
2352
2366
packet ['info' ]._set_as_published ()
2353
2367
@@ -3017,6 +3031,8 @@ def _handle_connack(self):
3017
3031
except Exception as err :
3018
3032
self ._easy_log (
3019
3033
MQTT_LOG_ERR , 'Caught exception in on_connect: %s' , err )
3034
+ if not self .suppress_exceptions :
3035
+ raise
3020
3036
3021
3037
if result == 0 :
3022
3038
rc = 0
@@ -3136,6 +3152,8 @@ def _handle_suback(self):
3136
3152
except Exception as err :
3137
3153
self ._easy_log (
3138
3154
MQTT_LOG_ERR , 'Caught exception in on_subscribe: %s' , err )
3155
+ if not self .suppress_exceptions :
3156
+ raise
3139
3157
3140
3158
return MQTT_ERR_SUCCESS
3141
3159
@@ -3323,6 +3341,8 @@ def _handle_unsuback(self):
3323
3341
except Exception as err :
3324
3342
self ._easy_log (
3325
3343
MQTT_LOG_ERR , 'Caught exception in on_unsubscribe: %s' , err )
3344
+ if not self .suppress_exceptions :
3345
+ raise
3326
3346
return MQTT_ERR_SUCCESS
3327
3347
3328
3348
def _do_on_disconnect (self , rc , properties = None ):
@@ -3338,6 +3358,8 @@ def _do_on_disconnect(self, rc, properties=None):
3338
3358
except Exception as err :
3339
3359
self ._easy_log (
3340
3360
MQTT_LOG_ERR , 'Caught exception in on_disconnect: %s' , err )
3361
+ if not self .suppress_exceptions :
3362
+ raise
3341
3363
3342
3364
def _do_on_publish (self , mid ):
3343
3365
with self ._callback_mutex :
@@ -3348,6 +3370,8 @@ def _do_on_publish(self, mid):
3348
3370
except Exception as err :
3349
3371
self ._easy_log (
3350
3372
MQTT_LOG_ERR , 'Caught exception in on_publish: %s' , err )
3373
+ if not self .suppress_exceptions :
3374
+ raise
3351
3375
3352
3376
msg = self ._out_messages .pop (mid )
3353
3377
msg .info ._set_as_published ()
@@ -3407,6 +3431,8 @@ def _handle_on_message(self, message):
3407
3431
callback .__name__ ,
3408
3432
err
3409
3433
)
3434
+ if not self .suppress_exceptions :
3435
+ raise
3410
3436
matched = True
3411
3437
3412
3438
if matched == False and self .on_message :
@@ -3416,6 +3442,8 @@ def _handle_on_message(self, message):
3416
3442
except Exception as err :
3417
3443
self ._easy_log (
3418
3444
MQTT_LOG_ERR , 'Caught exception in on_message: %s' , err )
3445
+ if not self .suppress_exceptions :
3446
+ raise
3419
3447
3420
3448
def _thread_main (self ):
3421
3449
self .loop_forever (retry_first_connection = True )
0 commit comments