Skip to content

Commit

Permalink
Add support for error callback for AlertListener (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc authored Nov 21, 2021
1 parent a8136d2 commit 15e011f
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions plexapi/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ class AlertListener(threading.Thread):
callback (func): Callback function to call on received messages. The callback function
will be sent a single argument 'data' which will contain a dictionary of data
received from the server. :samp:`def my_callback(data): ...`
callbackError (func): Callback function to call on errors. The callback function
will be sent a single argument 'error' which will contain the Error object.
:samp:`def my_callback(error): ...`
"""
key = '/:/websockets/notifications'

def __init__(self, server, callback=None):
def __init__(self, server, callback=None, callbackError=None):
super(AlertListener, self).__init__()
self.daemon = True
self._server = server
self._callback = callback
self._callbackError = callbackError
self._ws = None

def run(self):
Expand Down Expand Up @@ -84,4 +88,9 @@ def _onError(self, *args): # pragma: no cover
This is to support compatibility with current and previous releases of websocket-client.
"""
err = args[-1]
log.error('AlertListener Error: %s', err)
try:
log.error('AlertListener Error: %s', err)
if self._callbackError:
self._callbackError(err)
except Exception as err: # pragma: no cover
log.error('AlertListener Error: Error: %s', err)

0 comments on commit 15e011f

Please sign in to comment.