From 7405f078e655b19ff2302727647c8bcc5ba1b8ad Mon Sep 17 00:00:00 2001 From: mhchia Date: Tue, 24 Sep 2019 13:22:25 +0800 Subject: [PATCH] Raise `read_delim` exception with different msgs Separate `len(msg_bytes) == 0` and `msg_bytes[-1:] != b"\n"`, to raise `ParseError` with different messages. --- libp2p/utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libp2p/utils.py b/libp2p/utils.py index 4844f0e1..39c79e58 100644 --- a/libp2p/utils.py +++ b/libp2p/utils.py @@ -73,8 +73,12 @@ def encode_delim(msg: bytes) -> bytes: async def read_delim(reader: Reader) -> bytes: msg_bytes = await read_varint_prefixed_bytes(reader) - if len(msg_bytes) == 0 or msg_bytes[-1:] != b"\n": - raise ParseError(f'msg_bytes is not delimited by b"\\n": msg_bytes={msg_bytes}') + if len(msg_bytes) == 0: + raise ParseError(f"`len(msg_bytes)` should not be 0") + if msg_bytes[-1:] != b"\n": + raise ParseError( + f'`msg_bytes` is not delimited by b"\\n": `msg_bytes`={msg_bytes}' + ) return msg_bytes[:-1]