diff --git a/main.py b/main.py index 1b0aa1a..c8482da 100644 --- a/main.py +++ b/main.py @@ -35,7 +35,7 @@ import logbook from logbook import Logger, StderrHandler, StreamHandler import nio -from nio import TransportType, RemoteTransportError +from nio import TransportType, RemoteTransportError, RemoteProtocolError from matrix.colors import Formatted from matrix.utf import utf8_decode @@ -298,7 +298,7 @@ def receive_cb(server_name, file_descriptor): try: server.client.receive(data) - except RemoteTransportError as e: + except (RemoteTransportError, RemoteProtocolError) as e: server.error(str(e)) server.disconnect() diff --git a/matrix/server.py b/matrix/server.py index dd45754..d6c4ed6 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -24,7 +24,13 @@ import pprint from collections import deque, defaultdict -from nio import HttpClient, LoginResponse, SyncRepsponse +from nio import ( + HttpClient, + LoginResponse, + SyncRepsponse, + TransportResponse, + LocalProtocolError +) from matrix.plugin_options import Option, DebugType from matrix.utils import (key_from_value, prnt_debug, server_buffer_prnt, @@ -284,7 +290,7 @@ class MatrixServer: if self.server_buffer: buf = self.server_buffer - msg = "{prefix}{}: {}".format(SCRIPT_NAME, message) + msg = "{}{}: {}".format(W.prefix("network"), SCRIPT_NAME, message) W.prnt(buf, msg) def send(self, data): @@ -343,7 +349,11 @@ class MatrixServer: self.send_buffer = b"" self.current_message = None - self.client.disconnect() + + try: + self.client.disconnect() + except LocalProtocolError: + pass self.lag = 0 W.bar_item_update("lag") @@ -491,8 +501,12 @@ class MatrixServer: def handle_response(self, response): # type: (MatrixMessage) -> None + if isinstance(response, TransportResponse): + self.error("Error in response, code: {}".format( + response.status_code)) + self.disconnect() - if isinstance(response, LoginResponse): + elif isinstance(response, LoginResponse): self._handle_login(response) elif isinstance(response, SyncRepsponse): self._handle_sync(response)