From 8aebe4b883a97b64357b6218ee89aa098ca2dcae Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Mon, 22 Apr 2019 13:56:57 +0200 Subject: Create helper for socket error handling Changes send_to_websocket to handle the same errors as receive_ws_callback. --- wee_slack.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'wee_slack.py') diff --git a/wee_slack.py b/wee_slack.py index 1e86472..9839cf9 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -274,6 +274,19 @@ def get_functions_with_prefix(prefix): if name.startswith(prefix)} +def handle_socket_error(exception, team, caller_name): + if not (isinstance(exception, WebSocketConnectionClosedException) or + exception.errno in (errno.EPIPE, errno.ECONNRESET, errno.ETIMEDOUT)): + raise + + w.prnt(team.channel_buffer, + 'Lost connection to slack team {} (on {}), reconnecting.'.format( + team.domain, caller_name)) + dbg('Socket failed on {} with exception:\n{}'.format( + caller_name, format_exc_tb()), level=5) + team.set_disconnected() + + ###### New central Event router class EventRouter(object): @@ -408,13 +421,7 @@ class EventRouter(object): # Expected to happen occasionally on SSL websockets. return w.WEECHAT_RC_OK except (WebSocketConnectionClosedException, socket.error) as e: - if isinstance(e, WebSocketConnectionClosedException) or e.errno in (errno.EPIPE, errno.ECONNRESET, errno.ETIMEDOUT): - w.prnt(team.channel_buffer, - 'Lost connection to slack team {} (on receive), reconnecting.'.format(team.domain)) - dbg('receive_ws_callback failed with exception:\n{}'.format(format_exc_tb()), level=5) - team.set_disconnected() - else: - raise + handle_socket_error(e, team, 'receive') return w.WEECHAT_RC_OK message_json = json.loads(decode_from_utf8(data)) @@ -1288,12 +1295,8 @@ class SlackTeam(object): self.ws_replies[data["id"]] = data self.ws.send(encode_to_utf8(message)) dbg("Sent {}...".format(message[:100])) - except: - w.prnt(self.channel_buffer, - 'Lost connection to slack team {} (on send), reconnecting.'.format(self.domain)) - dbg('send_to_websocket failed with data: `{}` and exception:\n{}' - .format(message, format_exc_tb()), level=5) - self.set_disconnected() + except (WebSocketConnectionClosedException, socket.error) as e: + handle_socket_error(e, self, 'send') def update_member_presence(self, user, presence): user.presence = presence -- cgit