Better error messages for socket errors.

This commit is contained in:
poljar (Damir Jelić) 2018-02-02 09:50:08 +01:00
parent e9c027f131
commit f4c8f1ee29
2 changed files with 67 additions and 9 deletions

43
main.py
View file

@ -209,8 +209,21 @@ def try_ssl_handshake(server):
return False return False
except ssl.SSLError as error: except ssl.SSLError as error:
server_buffer_prnt(server, str(error)) str_error = error.reason if error.reason else "Unknown error"
matrix_server_reconnect(server)
message = ("{prefix}Error while doing SSL handshake"
": {error}").format(
prefix=W.prefix("network"),
error=str_error)
server_buffer_prnt(server, message)
server_buffer_prnt(
server,
("{prefix}matrix: disconnecting from server...").format(
prefix=W.prefix("network")))
matrix_server_disconnect(server)
return False return False
@ -224,6 +237,22 @@ def receive_cb(server_name, file_descriptor):
except ssl.SSLWantReadError: except ssl.SSLWantReadError:
break break
except socket.error as error: except socket.error as error:
errno = "error" + str(error.errno) + " " if error.errno else ""
str_error = error.strerror if error.strerror else "Unknown error"
str_error = errno + str_error
message = ("{prefix}Error while reading from "
"socket: {error}").format(
prefix=W.prefix("network"),
error=str_error)
server_buffer_prnt(server, message)
server_buffer_prnt(
server,
("{prefix}matrix: disconnecting from server...").format(
prefix=W.prefix("network")))
matrix_server_disconnect(server) matrix_server_disconnect(server)
# Queue the failed message for resending # Queue the failed message for resending
@ -231,11 +260,17 @@ def receive_cb(server_name, file_descriptor):
message = server.receive_queue.popleft() message = server.receive_queue.popleft()
server.send_queue.appendleft(message) server.send_queue.appendleft(message)
server_buffer_prnt(server, pprint.pformat(error))
return W.WEECHAT_RC_OK return W.WEECHAT_RC_OK
if not data: if not data:
server_buffer_prnt(server, "No data while reading") server_buffer_prnt(
server,
"{prefix}matrix: Error while reading from socket".format(
prefix=W.prefix("network")))
server_buffer_prnt(
server,
("{prefix}matrix: disconnecting from server...").format(
prefix=W.prefix("network")))
# Queue the failed message for resending # Queue the failed message for resending
if server.receive_queue: if server.receive_queue:

View file

@ -417,16 +417,39 @@ def try_send(server, message):
server.send_buffer = message[total_sent:] server.send_buffer = message[total_sent:]
return True return True
except OSError as error: except socket.error as error:
matrix_server_disconnect(server)
abort_send(server) abort_send(server)
server_buffer_prnt(server, str(error))
errno = "error" + str(error.errno) + " " if error.errno else ""
str_error = error.strerror if error.strerror else "Unknown reason"
str_error = errno + str_error
message = ("{prefix}Error while writing to "
"socket: {error}").format(
prefix=W.prefix("network"),
error=str_error)
server_buffer_prnt(server, message)
server_buffer_prnt(
server,
("{prefix}matrix: disconnecting from server...").format(
prefix=W.prefix("network")))
matrix_server_disconnect(server)
return False return False
if sent == 0: if sent == 0:
matrix_server_disconnect(server)
abort_send(server) abort_send(server)
server_buffer_prnt(server, "Socket closed while sending data.")
server_buffer_prnt(
server,
"{prefix}matrix: Error while writing to socket".format(
W.prefix("network")))
server_buffer_prnt(
server,
("{prefix}matrix: disconnecting from server...").format(
prefix=W.prefix("network")))
matrix_server_disconnect(server)
return False return False
total_sent = total_sent + sent total_sent = total_sent + sent