Better IPC exceptions
This commit is contained in:
parent
d861645d37
commit
2f0bbab635
@ -39,6 +39,17 @@ class IPCError(Exception):
|
|||||||
"""Exception raised when there was a problem with IPC."""
|
"""Exception raised when there was a problem with IPC."""
|
||||||
|
|
||||||
|
|
||||||
|
def _socket_error(action, socket):
|
||||||
|
"""Raise an IPCError based on an action and a QLocal{Socket,Server}.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
action: A string like "writing to running instance".
|
||||||
|
socket: A QLocalSocket or QLocalServer.
|
||||||
|
"""
|
||||||
|
raise IPCError("Error while {}: {} (error {})".format(
|
||||||
|
action, socket.errorString(), socket.error()))
|
||||||
|
|
||||||
|
|
||||||
def send_to_running_instance(cmdlist):
|
def send_to_running_instance(cmdlist):
|
||||||
"""Try to send a commandline to a running instance.
|
"""Try to send a commandline to a running instance.
|
||||||
|
|
||||||
@ -59,13 +70,13 @@ def send_to_running_instance(cmdlist):
|
|||||||
socket.writeData(line.encode('utf-8'))
|
socket.writeData(line.encode('utf-8'))
|
||||||
socket.waitForBytesWritten(WRITE_TIMEOUT)
|
socket.waitForBytesWritten(WRITE_TIMEOUT)
|
||||||
if socket.error() != QLocalSocket.UnknownError:
|
if socket.error() != QLocalSocket.UnknownError:
|
||||||
raise IPCError("Error while writing to running instance: "
|
_socket_error("writing to running instance", socket)
|
||||||
"{}".format(socket.errorString()))
|
else:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
if socket.error() != QLocalSocket.ConnectionRefusedError:
|
if socket.error() != QLocalSocket.ConnectionRefusedError:
|
||||||
raise IPCError("Error while connecting to running instance: "
|
_socket_error("connecting to running instance", socket)
|
||||||
"{}".format(socket.errorString()))
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@ -77,8 +88,7 @@ def init_server():
|
|||||||
server = QLocalServer()
|
server = QLocalServer()
|
||||||
ok = server.listen(SOCKETNAME)
|
ok = server.listen(SOCKETNAME)
|
||||||
if not ok:
|
if not ok:
|
||||||
raise IPCError("Error while listening to local socket: {}".format(
|
_socket_error("listening to local server", server)
|
||||||
server.errorString()))
|
|
||||||
server.newConnection.connect(on_localsocket_connection)
|
server.newConnection.connect(on_localsocket_connection)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user