From 55298e49caa5d76a7371568d2773e8e245b388a1 Mon Sep 17 00:00:00 2001 From: Jason Joy Atsu Winmorre <78548837+winmorre@users.noreply.github.com> Date: Sun, 25 Dec 2022 13:28:53 +0000 Subject: 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 --- CHANGES | 1 + redis/connection.py | 34 +++++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/CHANGES b/CHANGES index 72b896b..228910f 100644 --- a/CHANGES +++ b/CHANGES @@ -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) diff --git a/redis/connection.py b/redis/connection.py index dce0735..b810fc5 100755 --- a/redis/connection.py +++ b/redis/connection.py @@ -664,12 +664,23 @@ class Connection: 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]}" @@ -677,7 +688,7 @@ class Connection: 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]}" @@ -793,29 +804,30 @@ class Connection: 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 -- cgit v1.2.1