Skip to content

Commit 15e011f

Browse files
authored
Add support for error callback for AlertListener (#850)
1 parent a8136d2 commit 15e011f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

plexapi/alert.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ class AlertListener(threading.Thread):
2929
callback (func): Callback function to call on received messages. The callback function
3030
will be sent a single argument 'data' which will contain a dictionary of data
3131
received from the server. :samp:`def my_callback(data): ...`
32+
callbackError (func): Callback function to call on errors. The callback function
33+
will be sent a single argument 'error' which will contain the Error object.
34+
:samp:`def my_callback(error): ...`
3235
"""
3336
key = '/:/websockets/notifications'
3437

35-
def __init__(self, server, callback=None):
38+
def __init__(self, server, callback=None, callbackError=None):
3639
super(AlertListener, self).__init__()
3740
self.daemon = True
3841
self._server = server
3942
self._callback = callback
43+
self._callbackError = callbackError
4044
self._ws = None
4145

4246
def run(self):
@@ -84,4 +88,9 @@ def _onError(self, *args): # pragma: no cover
8488
This is to support compatibility with current and previous releases of websocket-client.
8589
"""
8690
err = args[-1]
87-
log.error('AlertListener Error: %s', err)
91+
try:
92+
log.error('AlertListener Error: %s', err)
93+
if self._callbackError:
94+
self._callbackError(err)
95+
except Exception as err: # pragma: no cover
96+
log.error('AlertListener Error: Error: %s', err)

0 commit comments

Comments
 (0)