diff options
author | Jason Joy Atsu Winmorre <78548837+winmorre@users.noreply.github.com> | 2022-12-25 13:28:53 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-25 15:28:53 +0200 |
commit | 55298e49caa5d76a7371568d2773e8e245b388a1 (patch) | |
tree | 4e06245e3169272605d9c1193c2458575e702153 /redis | |
parent | 1000a2b3c04570e6d011a6dcad2ec5299ff00563 (diff) | |
download | redis-py-55298e49caa5d76a7371568d2773e8e245b388a1.tar.gz |
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
Diffstat (limited to 'redis')
-rwxr-xr-x | redis/connection.py | 34 |
1 files changed, 23 insertions, 11 deletions
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 |