Skip to content

Commit 55298e4

Browse files
authored
Fix for Unhandled exception related to self.host with unix socket (#2520)
* Fix for Unhandled exception related to self.host with unix socket * Added change to the CHANGES file * fix linter error * Reformatted connection.py file
1 parent 1000a2b commit 55298e4

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Enable Lock for asyncio cluster mode
3232
* Fix Sentinel.execute_command doesn't execute across the entire sentinel cluster bug (#2458)
3333
* Added a replacement for the default cluster node in the event of failure (#2463)
34+
* Fix for Unhandled exception related to self.host with unix socket (#2496)
3435

3536
* 4.1.3 (Feb 8, 2022)
3637
* Fix flushdb and flushall (#1926)

redis/connection.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -664,20 +664,31 @@ def _connect(self):
664664
raise err
665665
raise OSError("socket.getaddrinfo returned an empty list")
666666

667+
def _host_error(self):
668+
try:
669+
host_error = f"{self.host}:{self.port}"
670+
except AttributeError:
671+
host_error = "connection"
672+
673+
return host_error
674+
667675
def _error_message(self, exception):
668676
# args for socket.error can either be (errno, "message")
669677
# or just "message"
678+
679+
host_error = self._host_error()
680+
670681
if len(exception.args) == 1:
671682
try:
672-
return f"Error connecting to {self.host}:{self.port}. \
683+
return f"Error connecting to {host_error}. \
673684
{exception.args[0]}."
674685
except AttributeError:
675686
return f"Connection Error: {exception.args[0]}"
676687
else:
677688
try:
678689
return (
679690
f"Error {exception.args[0]} connecting to "
680-
f"{self.host}:{self.port}. {exception.args[1]}."
691+
f"{host_error}. {exception.args[1]}."
681692
)
682693
except AttributeError:
683694
return f"Connection Error: {exception.args[0]}"
@@ -793,29 +804,30 @@ def can_read(self, timeout=0):
793804
sock = self._sock
794805
if not sock:
795806
self.connect()
807+
808+
host_error = self._host_error()
809+
796810
try:
797811
return self._parser.can_read(timeout)
798812
except OSError as e:
799813
self.disconnect()
800-
raise ConnectionError(
801-
f"Error while reading from {self.host}:{self.port}: {e.args}"
802-
)
814+
raise ConnectionError(f"Error while reading from {host_error}: {e.args}")
803815

804816
def read_response(self, disable_decoding=False):
805817
"""Read the response from a previously sent command"""
806-
try:
807-
hosterr = f"{self.host}:{self.port}"
808-
except AttributeError:
809-
hosterr = "connection"
818+
819+
host_error = self._host_error()
810820

811821
try:
812822
response = self._parser.read_response(disable_decoding=disable_decoding)
813823
except socket.timeout:
814824
self.disconnect()
815-
raise TimeoutError(f"Timeout reading from {hosterr}")
825+
raise TimeoutError(f"Timeout reading from {host_error}")
816826
except OSError as e:
817827
self.disconnect()
818-
raise ConnectionError(f"Error while reading from {hosterr}" f" : {e.args}")
828+
raise ConnectionError(
829+
f"Error while reading from {host_error}" f" : {e.args}"
830+
)
819831
except Exception:
820832
self.disconnect()
821833
raise

0 commit comments

Comments
 (0)