@@ -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