2424
2525class ERROR_MSGS (str , Enum ):
2626 WRONG_FILE_TYPE = "'%s' is not a valid type to be opened as a file"
27- ADDRESS_TUPLE = "Devo-SenderConfigSSL| address must be a tuple (\" hostname\" , int(port))'" ,
28- WRONG_SSL_CONFIG = "Devo-SenderConfigSSL|Can't create SSL config: %s" ,
29- CONFIG_FILE_NOT_FOUND = "Error in the configuration, %s is not a file or the path does not exist" ,
30- CANT_READ_CONFIG_FILE = "Error in the configuration %s can't be read\n original error: %s" ,
27+ ADDRESS_TUPLE = "Devo-SenderConfigSSL| address must be a tuple (\" hostname\" , int(port))'"
28+ WRONG_SSL_CONFIG = "Devo-SenderConfigSSL|Can't create SSL config: %s"
29+ CONFIG_FILE_NOT_FOUND = "Error in the configuration, %s is not a file or the path does not exist"
30+ CANT_READ_CONFIG_FILE = "Error in the configuration %s can't be read\n original error: %s"
3131 CONFIG_FILE_PROBLEM = "Error in the configuration, %s problem related to: %s"
32+ KEY_NOT_COMPATIBLE_WITH_CERT = "Error in the configuration, the key: %s is not compatible with the cert: %s\n original error: %s"
33+ CHAIN_NOT_COMPATIBLE_WITH_CERT = "Error in config, the chain: %s is not compatible with the certificate: %s\n original error: %s"
34+ TIMEOUT_RELATED_TO_AN_INCORRECT_ADDRESS_PORT = "Possible error in config, a timeout could be related to an incorrect address/port: %s\n original error: %s"
35+ INCORRECT_ADDRESS_PORT = "Error in config, incorrect address/port: %s\n original error: %s"
36+ CERTIFICATE_IN_ADDRESS_IS_NOT_COMPATIBLE = "Error in config, the certificate in the address: %s is not compatible with: %s"
37+ ADDRESS_MUST_BE_A_TUPLE = "Devo-SenderConfigSSL| address must be a tuple '(\" hostname\" , int(port))'"
38+ CANT_CREATE_TCP_CONFIG = "DevoSenderConfigTCP|Can't create TCP config: %s"
39+ PROBLEMS_WITH_SENDER_ARGS = "Problems with args passed to Sender"
40+ TCP_CONN_ESTABLISHMENT_SOCKET = "TCP conn establishment socket error: %s"
41+ SSL_CONN_ESTABLISHMENT_SOCKET = "SSL conn establishment socket error: %s"
42+ PFX_CERTIFICATE_READ_FAILED = "PFX Certificate read failed: %s"
43+ SEND_ERROR = "Send error"
44+ SOCKET_ERROR = "Socket error: %s"
45+ SOCKET_CANT_CONNECT_UNKNOWN_ERROR = "Socket cant connect: unknown error"
46+ NO_ADDRESS = "No address"
3247
3348
3449class DevoSenderException (Exception ):
@@ -142,9 +157,9 @@ def check_config_certificate_key(self):
142157 context .check_privatekey ()
143158 except SSL .Error as message :
144159 raise DevoSenderException (
145- "Error in the configuration, the key: " + self . key +
146- " is not compatible with the cert: " + self .cert +
147- " \n original error: " + str ( message )) from message
160+ ERROR_MSGS . KEY_NOT_COMPATIBLE_WITH_CERT % (
161+ self . key , self .cert , str ( message )
162+ )) from message
148163 return True
149164
150165 def check_config_certificate_chain (self ):
@@ -172,9 +187,9 @@ def check_config_certificate_chain(self):
172187 store_ctx .verify_certificate ()
173188 except crypto .X509StoreContextError as message :
174189 raise DevoSenderException (
175- "Error in config, the chain: " + self . chain +
176- " is not compatible with the certificate: " + self .cert +
177- " \n original error: " + str ( message )) from message
190+ ERROR_MSGS . CHAIN_NOT_COMPATIBLE_WITH_CERT % (
191+ self . chain , self .cert , str ( message )
192+ )) from message
178193 return True
179194
180195 def check_config_certificate_address (self ):
@@ -193,14 +208,14 @@ def check_config_certificate_address(self):
193208 connection .connect (self .address )
194209 except socket .timeout as message :
195210 raise DevoSenderException (
196- "Possible error in config, a timeout could be related " +
197- "to an incorrect address/port: " + str (self .address ) +
198- " \n original error: " + str ( message )) from message
211+ ERROR_MSGS . TIMEOUT_RELATED_TO_AN_INCORRECT_ADDRESS_PORT % (
212+ str (self .address ), str ( message )
213+ )) from message
199214 except ConnectionRefusedError as message :
200215 raise DevoSenderException (
201- "Error in config, incorrect address/port: "
202- + str (self .address ) +
203- " \n original error: " + str ( message )) from message
216+ ERROR_MSGS . INCORRECT_ADDRESS_PORT % (
217+ str (self .address ), str ( message )
218+ )) from message
204219 sock .setblocking (True )
205220 connection .do_handshake ()
206221 server_chain = connection .get_peer_cert_chain ()
@@ -222,10 +237,9 @@ def check_config_certificate_address(self):
222237 return True
223238
224239 raise DevoSenderException (
225- "Error in config, the certificate in the address: "
226- + self .address [0 ] +
227- " is not compatible with: " +
228- self .chain )
240+ ERROR_MSGS .CERTIFICATE_IN_ADDRESS_IS_NOT_COMPATIBLE % (
241+ self .address [0 ], self .chain
242+ ))
229243
230244 @staticmethod
231245 def get_common_names (cert_chain , components_type ):
@@ -261,17 +275,14 @@ class SenderConfigTCP:
261275
262276 def __init__ (self , address = None ):
263277 if not isinstance (address , tuple ):
264- raise DevoSenderException (
265- "Devo-SenderConfigSSL| address must be a tuple "
266- "'(\" hostname\" , int(port))'" )
278+ raise DevoSenderException (ERROR_MSGS .ADDRESS_MUST_BE_A_TUPLE )
267279 try :
268280 self .address = address
269281 self .hostname = socket .gethostname ()
270282 self .sec_level = None
271283 except Exception as error :
272284 raise DevoSenderException (
273- "DevoSenderConfigTCP|Can't create TCP config: "
274- "%s" % str (error )) from error
285+ ERROR_MSGS .CANT_CREATE_TCP_CONFIG % str (error )) from error
275286
276287
277288class SenderBuffer :
@@ -299,7 +310,7 @@ class Sender(logging.Handler):
299310 def __init__ (self , config = None , con_type = None ,
300311 timeout = 30 , debug = False , logger = None ):
301312 if config is None :
302- raise DevoSenderException ("Problems with args passed to Sender" )
313+ raise DevoSenderException (ERROR_MSGS . PROBLEMS_WITH_SENDER_ARGS )
303314
304315 self .socket = None
305316 self .reconnection = 0
@@ -354,8 +365,7 @@ def __connect_tcp_socket(self):
354365 except socket .error as error :
355366 self .close ()
356367 raise DevoSenderException (
357- "TCP conn establishment socket error: %s" % str (
358- error )) from error
368+ ERROR_MSGS .TCP_CONN_ESTABLISHMENT_SOCKET % str (error )) from error
359369
360370 self .timestart = int (round (time .time () * 1000 ))
361371
@@ -380,8 +390,7 @@ def __connect_ssl(self):
380390 except Exception as error :
381391 self .close ()
382392 raise DevoSenderException (
383- "PFX Certificate read failed: %s" %
384- str (error )) from error
393+ ERROR_MSGS .PFX_CERTIFICATE_READ_FAILED % str (error )) from error
385394 try :
386395 try :
387396 if self ._sender_config .key is not None \
@@ -425,8 +434,7 @@ def __connect_ssl(self):
425434 except socket .error as error :
426435 self .close ()
427436 raise DevoSenderException (
428- "SSL conn establishment socket error: %s" %
429- str (error )) from error
437+ ERROR_MSGS .SSL_CONN_ESTABLISHMENT_SOCKET % str (error )) from error
430438
431439 def info (self , msg ):
432440 """
@@ -562,10 +570,10 @@ def __send_oc(self, record):
562570 part = record [int (iteration * 4096 ):
563571 int ((iteration + 1 ) * 4096 )]
564572 if self .socket .sendall (part ) is not None :
565- raise DevoSenderException ("Send error" )
573+ raise DevoSenderException (ERROR_MSGS . SEND_ERROR )
566574 sent += len (part )
567575 if sent == 0 :
568- raise DevoSenderException ("Send error" )
576+ raise DevoSenderException (ERROR_MSGS . SEND_ERROR )
569577 return sent
570578
571579 def send_raw (self , record , multiline = False , zip = False ):
@@ -586,7 +594,7 @@ def send_raw(self, record, multiline=False, zip=False):
586594 msg = self .__encode_record (record )
587595 sent = len (msg )
588596 if self .socket .sendall (msg ) is not None :
589- raise DevoSenderException ("Send error" )
597+ raise DevoSenderException (ERROR_MSGS . SEND_ERROR )
590598 return 1
591599 if multiline :
592600 record = self .__encode_multiline (record )
@@ -595,15 +603,15 @@ def send_raw(self, record, multiline=False, zip=False):
595603 if sent :
596604 return 1
597605 return 0
598- except socket .error :
606+ except socket .error as error :
599607 self .close ()
600608 raise DevoSenderException (
601- "Socket error: %s" % str (socket . error )) from error
609+ ERROR_MSGS . SOCKET_ERROR % str (error )) from error
602610 finally :
603611 if self .debug :
604612 self .logger .debug ('sent|%d|size|%d|msg|%s' %
605613 (sent , len (record ), record ))
606- raise Exception ("Socket cant connect: unknown error" )
614+ raise Exception (ERROR_MSGS . SOCKET_CANT_CONNECT_UNKNOWN_ERROR )
607615 except Exception as error :
608616 raise DevoSenderException (error ) from error
609617
@@ -773,7 +781,7 @@ def _from_dict(config=None, con_type=None):
773781 address = config .get ("address" , None )
774782
775783 if not address :
776- raise DevoSenderException ("No address" )
784+ raise DevoSenderException (ERROR_MSGS . NO_ADDRESS )
777785
778786 if not isinstance (address , tuple ):
779787 address = (address , int (config .get ("port" , 443 )))
0 commit comments