Raise MultiselectCommunicatorError:

when failed to write to communicator
This commit is contained in:
NIC619 2019-09-19 21:51:23 +08:00
parent b9d1875027
commit c6294ad19b
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17
3 changed files with 26 additions and 6 deletions

View File

@ -59,12 +59,18 @@ class Multiselect(IMultiselectMuxer):
protocol = TProtocol(command)
if protocol in self.handlers:
# Tell counterparty we have decided on a protocol
try:
await communicator.write(protocol)
except MultiselectCommunicatorError as error:
raise MultiselectError(error)
# Return the decided on protocol
return protocol, self.handlers[protocol]
# Tell counterparty this protocol was not found
try:
await communicator.write(PROTOCOL_NOT_FOUND_MSG)
except MultiselectCommunicatorError as error:
raise MultiselectError(error)
async def handshake(self, communicator: IMultiselectCommunicator) -> None:
"""
@ -76,7 +82,10 @@ class Multiselect(IMultiselectMuxer):
# TODO: Use format used by go repo for messages
# Send our MULTISELECT_PROTOCOL_ID to other party
try:
await communicator.write(MULTISELECT_PROTOCOL_ID)
except MultiselectCommunicatorError as error:
raise MultiselectError(error)
# Read in the protocol ID from other party
try:

View File

@ -27,7 +27,10 @@ class MultiselectClient(IMultiselectClient):
# TODO: Use format used by go repo for messages
# Send our MULTISELECT_PROTOCOL_ID to counterparty
try:
await communicator.write(MULTISELECT_PROTOCOL_ID)
except MultiselectCommunicatorError as error:
raise MultiselectClientError(error)
# Read in the protocol ID from other party
try:
@ -79,7 +82,10 @@ class MultiselectClient(IMultiselectClient):
"""
# Tell counterparty we want to use protocol
try:
await communicator.write(protocol)
except MultiselectCommunicatorError as error:
raise MultiselectClientError(error)
# Get what counterparty says in response
try:

View File

@ -15,7 +15,12 @@ class MultiselectCommunicator(IMultiselectCommunicator):
async def write(self, msg_str: str) -> None:
msg_bytes = encode_delim(msg_str.encode())
try:
await self.read_writer.write(msg_bytes)
except IOException:
raise MultiselectCommunicatorError(
"fail to write to multiselect communicator"
)
async def read(self) -> str:
try: