@@ -664,20 +664,31 @@ def _connect(self):
664
664
raise err
665
665
raise OSError ("socket.getaddrinfo returned an empty list" )
666
666
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
+
667
675
def _error_message (self , exception ):
668
676
# args for socket.error can either be (errno, "message")
669
677
# or just "message"
678
+
679
+ host_error = self ._host_error ()
680
+
670
681
if len (exception .args ) == 1 :
671
682
try :
672
- return f"Error connecting to { self . host } : { self . port } . \
683
+ return f"Error connecting to { host_error } . \
673
684
{ exception .args [0 ]} ."
674
685
except AttributeError :
675
686
return f"Connection Error: { exception .args [0 ]} "
676
687
else :
677
688
try :
678
689
return (
679
690
f"Error { exception .args [0 ]} connecting to "
680
- f"{ self . host } : { self . port } . { exception .args [1 ]} ."
691
+ f"{ host_error } . { exception .args [1 ]} ."
681
692
)
682
693
except AttributeError :
683
694
return f"Connection Error: { exception .args [0 ]} "
@@ -793,29 +804,30 @@ def can_read(self, timeout=0):
793
804
sock = self ._sock
794
805
if not sock :
795
806
self .connect ()
807
+
808
+ host_error = self ._host_error ()
809
+
796
810
try :
797
811
return self ._parser .can_read (timeout )
798
812
except OSError as e :
799
813
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 } " )
803
815
804
816
def read_response (self , disable_decoding = False ):
805
817
"""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 ()
810
820
811
821
try :
812
822
response = self ._parser .read_response (disable_decoding = disable_decoding )
813
823
except socket .timeout :
814
824
self .disconnect ()
815
- raise TimeoutError (f"Timeout reading from { hosterr } " )
825
+ raise TimeoutError (f"Timeout reading from { host_error } " )
816
826
except OSError as e :
817
827
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
+ )
819
831
except Exception :
820
832
self .disconnect ()
821
833
raise
0 commit comments