Skip to content

Commit

Permalink
event listener: handle all exceptions better
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed Mar 15, 2023
1 parent fa62dcb commit c633a60
Showing 1 changed file with 7 additions and 31 deletions.
38 changes: 7 additions & 31 deletions linkedin_matrix/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,10 @@ def on_listen_task_end(self, future: Future):
and self.client
and not self.shutdown
):
self._track_metric(METRIC_CONNECTED, False)
self.log.warn("Logged out, but not by a logout call, sending bad credentials.")
asyncio.create_task(self.push_bridge_state(BridgeStateEvent.BAD_CREDENTIALS))
future.cancel()

listener_event_handlers_created: bool = False
listener_task_i: int = 0
Expand All @@ -528,21 +530,20 @@ async def _try_listen(self):
return
if not self.listener_event_handlers_created:
self.client.add_event_listener("ALL_EVENTS", self.handle_linkedin_stream_event)
self.client.add_event_listener("STREAM_ERROR", self.handle_linkedin_listener_error)
self.client.add_event_listener("event", self.handle_linkedin_event)
self.client.add_event_listener("reactionAdded", self.handle_linkedin_reaction_added)
self.client.add_event_listener("action", self.handle_linkedin_action)
self.client.add_event_listener("fromEntity", self.handle_linkedin_from_entity)
self.listener_event_handlers_created = True
try:
await self.client.start_listener()
except TooManyRedirects as e:
self.log.exception(
"Too many redirects. This is likely due to being logged out from another session.",
e,
)
except Exception as e:
self.log.exception(f"Exception in listener: {e}")
self._is_logged_in = False
self._is_connected = False
self.li_member_urn = None
self._prev_connected_bridge_state = -600
self.user_profile_cache = None

_prev_connected_bridge_state = -600

Expand All @@ -558,31 +559,6 @@ async def handle_linkedin_stream_event(self, _):
self._track_metric(METRIC_CONNECTED, True)
await self._push_connected_state()

async def handle_linkedin_listener_error(self, error: Exception):
self._track_metric(METRIC_CONNECTED, False)
self._prev_connected_bridge_state = -600
self.user_profile_cache = None

if isinstance(error, TooManyRedirects):
# This means that the user's session is borked (the redirects mean it's
# trying to redirect to the login page).
await self.push_bridge_state(
BridgeStateEvent.BAD_CREDENTIALS,
message=f"TooManyRedirects: {error}",
)
if self.listen_task:
self.client = None
self.user_profile_cache = None
self.li_member_urn = None
self._is_logged_in = False
self._prev_connected_bridge_state = -600
self.stop_listen()
else:
await self.push_bridge_state(
BridgeStateEvent.TRANSIENT_DISCONNECT,
message=str(error),
)

async def handle_linkedin_event(self, event: RealTimeEventStreamEvent):
assert self.client
assert isinstance(event.event, ConversationEvent)
Expand Down

0 comments on commit c633a60

Please sign in to comment.