Skip to content

Commit

Permalink
Replace OSError exceptions from can_read with `redis.ConnectionErro…
Browse files Browse the repository at this point in the history
…r` (#2140)

* Replace OSError exceptions from `can_read` with `redis.ConnectionError`

* Fix formatting

* Revert unintended formatting change
  • Loading branch information
kristjanvalur authored May 8, 2022
1 parent 963843b commit c25be04
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion redis/asyncio/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,13 @@ async def can_read(self, timeout: float = 0):
"""Poll the socket to see if there's data that can be read."""
if not self.is_connected:
await self.connect()
return await self._parser.can_read(timeout)
try:
return await self._parser.can_read(timeout)
except OSError as e:
await self.disconnect()
raise ConnectionError(
f"Error while reading from {self.host}:{self.port}: {e.args}"
)

async def read_response(self, disable_decoding: bool = False):
"""Read the response from a previously sent command"""
Expand Down
8 changes: 7 additions & 1 deletion redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,13 @@ def can_read(self, timeout=0):
sock = self._sock
if not sock:
self.connect()
return self._parser.can_read(timeout)
try:
return self._parser.can_read(timeout)
except OSError as e:
self.disconnect()
raise ConnectionError(
f"Error while reading from {self.host}:{self.port}: {e.args}"
)

def read_response(self, disable_decoding=False):
"""Read the response from a previously sent command"""
Expand Down

0 comments on commit c25be04

Please sign in to comment.