From f200ba90d9010fedf26b7af5f56ede6e48128ed2 Mon Sep 17 00:00:00 2001 From: Nawar Noori Date: Mon, 13 Jul 2020 11:19:14 +0100 Subject: [PATCH] Just raise ReadError directly. --- httpcore/_backends/asyncio.py | 4 +++- httpcore/_backends/sync.py | 4 +++- httpcore/_backends/trio.py | 10 ++++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/httpcore/_backends/asyncio.py b/httpcore/_backends/asyncio.py index cc7f3ea..9764cfa 100644 --- a/httpcore/_backends/asyncio.py +++ b/httpcore/_backends/asyncio.py @@ -135,7 +135,9 @@ class SocketStream(AsyncSocketStream): self.stream_reader.read(n), timeout.get("read") ) if data == b"": - raise OSError("Server disconnected while attempting read") + raise ReadError( + "Server unexpectedly disconnected while attempting read." + ) return data async def write(self, data: bytes, timeout: TimeoutDict) -> None: diff --git a/httpcore/_backends/sync.py b/httpcore/_backends/sync.py index 42d5ea3..2139de9 100644 --- a/httpcore/_backends/sync.py +++ b/httpcore/_backends/sync.py @@ -61,7 +61,9 @@ class SyncSocketStream: self.sock.settimeout(read_timeout) data = self.sock.recv(n) if data == b"": - raise OSError("Server disconnected while attempting read") + raise ReadError( + "Server unexpectedly disconnected while attempting read." + ) return data def write(self, data: bytes, timeout: TimeoutDict) -> None: diff --git a/httpcore/_backends/trio.py b/httpcore/_backends/trio.py index 19725cd..6668f69 100644 --- a/httpcore/_backends/trio.py +++ b/httpcore/_backends/trio.py @@ -55,19 +55,21 @@ class SocketStream(AsyncSocketStream): async def read(self, n: int, timeout: TimeoutDict) -> bytes: read_timeout = none_as_inf(timeout.get("read")) - exc_map = {trio.TooSlowError: ReadTimeout, OSError: ReadError} + exc_map = {trio.TooSlowError: ReadTimeout} async with self.read_lock: with map_exceptions(exc_map): with trio.fail_after(read_timeout): + err_msg = ( + "Server unexpectedly disconnected while attempting read." + ) try: data = await self.stream.receive_some(max_bytes=n) except trio.BrokenResourceError: - message = "Server disconnected while attempting read" - raise OSError(message) from None + raise ReadError(err_msg) from None if data == b"": - raise OSError("Server disconnected while attempting read") + raise ReadError(err_msg) return data -- 2.27.0