Skip to content

Commit

Permalink
Fix for Unhandled exception related to self.host with unix socket (#2520
Browse files Browse the repository at this point in the history
)

* Fix for Unhandled exception related to self.host with unix socket

* Added change to the CHANGES file

* fix linter error

* Reformatted connection.py file
  • Loading branch information
winmorre committed Dec 25, 2022
1 parent 1000a2b commit 55298e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* Enable Lock for asyncio cluster mode
* Fix Sentinel.execute_command doesn't execute across the entire sentinel cluster bug (#2458)
* Added a replacement for the default cluster node in the event of failure (#2463)
* Fix for Unhandled exception related to self.host with unix socket (#2496)

* 4.1.3 (Feb 8, 2022)
* Fix flushdb and flushall (#1926)
Expand Down
34 changes: 23 additions & 11 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,20 +664,31 @@ def _connect(self):
raise err
raise OSError("socket.getaddrinfo returned an empty list")

def _host_error(self):
try:
host_error = f"{self.host}:{self.port}"
except AttributeError:
host_error = "connection"

return host_error

def _error_message(self, exception):
# args for socket.error can either be (errno, "message")
# or just "message"

host_error = self._host_error()

if len(exception.args) == 1:
try:
return f"Error connecting to {self.host}:{self.port}. \
return f"Error connecting to {host_error}. \
{exception.args[0]}."
except AttributeError:
return f"Connection Error: {exception.args[0]}"
else:
try:
return (
f"Error {exception.args[0]} connecting to "
f"{self.host}:{self.port}. {exception.args[1]}."
f"{host_error}. {exception.args[1]}."
)
except AttributeError:
return f"Connection Error: {exception.args[0]}"
Expand Down Expand Up @@ -793,29 +804,30 @@ def can_read(self, timeout=0):
sock = self._sock
if not sock:
self.connect()

host_error = self._host_error()

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}"
)
raise ConnectionError(f"Error while reading from {host_error}: {e.args}")

def read_response(self, disable_decoding=False):
"""Read the response from a previously sent command"""
try:
hosterr = f"{self.host}:{self.port}"
except AttributeError:
hosterr = "connection"

host_error = self._host_error()

try:
response = self._parser.read_response(disable_decoding=disable_decoding)
except socket.timeout:
self.disconnect()
raise TimeoutError(f"Timeout reading from {hosterr}")
raise TimeoutError(f"Timeout reading from {host_error}")
except OSError as e:
self.disconnect()
raise ConnectionError(f"Error while reading from {hosterr}" f" : {e.args}")
raise ConnectionError(
f"Error while reading from {host_error}" f" : {e.args}"
)
except Exception:
self.disconnect()
raise
Expand Down

0 comments on commit 55298e4

Please sign in to comment.