24
24
25
25
class ERROR_MSGS (str , Enum ):
26
26
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"
31
31
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"
32
47
33
48
34
49
class DevoSenderException (Exception ):
@@ -142,9 +157,9 @@ def check_config_certificate_key(self):
142
157
context .check_privatekey ()
143
158
except SSL .Error as message :
144
159
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
148
163
return True
149
164
150
165
def check_config_certificate_chain (self ):
@@ -172,9 +187,9 @@ def check_config_certificate_chain(self):
172
187
store_ctx .verify_certificate ()
173
188
except crypto .X509StoreContextError as message :
174
189
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
178
193
return True
179
194
180
195
def check_config_certificate_address (self ):
@@ -193,14 +208,14 @@ def check_config_certificate_address(self):
193
208
connection .connect (self .address )
194
209
except socket .timeout as message :
195
210
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
199
214
except ConnectionRefusedError as message :
200
215
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
204
219
sock .setblocking (True )
205
220
connection .do_handshake ()
206
221
server_chain = connection .get_peer_cert_chain ()
@@ -222,10 +237,9 @@ def check_config_certificate_address(self):
222
237
return True
223
238
224
239
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
+ ))
229
243
230
244
@staticmethod
231
245
def get_common_names (cert_chain , components_type ):
@@ -261,17 +275,14 @@ class SenderConfigTCP:
261
275
262
276
def __init__ (self , address = None ):
263
277
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 )
267
279
try :
268
280
self .address = address
269
281
self .hostname = socket .gethostname ()
270
282
self .sec_level = None
271
283
except Exception as error :
272
284
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
275
286
276
287
277
288
class SenderBuffer :
@@ -299,7 +310,7 @@ class Sender(logging.Handler):
299
310
def __init__ (self , config = None , con_type = None ,
300
311
timeout = 30 , debug = False , logger = None ):
301
312
if config is None :
302
- raise DevoSenderException ("Problems with args passed to Sender" )
313
+ raise DevoSenderException (ERROR_MSGS . PROBLEMS_WITH_SENDER_ARGS )
303
314
304
315
self .socket = None
305
316
self .reconnection = 0
@@ -354,8 +365,7 @@ def __connect_tcp_socket(self):
354
365
except socket .error as error :
355
366
self .close ()
356
367
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
359
369
360
370
self .timestart = int (round (time .time () * 1000 ))
361
371
@@ -380,8 +390,7 @@ def __connect_ssl(self):
380
390
except Exception as error :
381
391
self .close ()
382
392
raise DevoSenderException (
383
- "PFX Certificate read failed: %s" %
384
- str (error )) from error
393
+ ERROR_MSGS .PFX_CERTIFICATE_READ_FAILED % str (error )) from error
385
394
try :
386
395
try :
387
396
if self ._sender_config .key is not None \
@@ -425,8 +434,7 @@ def __connect_ssl(self):
425
434
except socket .error as error :
426
435
self .close ()
427
436
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
430
438
431
439
def info (self , msg ):
432
440
"""
@@ -562,10 +570,10 @@ def __send_oc(self, record):
562
570
part = record [int (iteration * 4096 ):
563
571
int ((iteration + 1 ) * 4096 )]
564
572
if self .socket .sendall (part ) is not None :
565
- raise DevoSenderException ("Send error" )
573
+ raise DevoSenderException (ERROR_MSGS . SEND_ERROR )
566
574
sent += len (part )
567
575
if sent == 0 :
568
- raise DevoSenderException ("Send error" )
576
+ raise DevoSenderException (ERROR_MSGS . SEND_ERROR )
569
577
return sent
570
578
571
579
def send_raw (self , record , multiline = False , zip = False ):
@@ -586,7 +594,7 @@ def send_raw(self, record, multiline=False, zip=False):
586
594
msg = self .__encode_record (record )
587
595
sent = len (msg )
588
596
if self .socket .sendall (msg ) is not None :
589
- raise DevoSenderException ("Send error" )
597
+ raise DevoSenderException (ERROR_MSGS . SEND_ERROR )
590
598
return 1
591
599
if multiline :
592
600
record = self .__encode_multiline (record )
@@ -595,15 +603,15 @@ def send_raw(self, record, multiline=False, zip=False):
595
603
if sent :
596
604
return 1
597
605
return 0
598
- except socket .error :
606
+ except socket .error as error :
599
607
self .close ()
600
608
raise DevoSenderException (
601
- "Socket error: %s" % str (socket . error )) from error
609
+ ERROR_MSGS . SOCKET_ERROR % str (error )) from error
602
610
finally :
603
611
if self .debug :
604
612
self .logger .debug ('sent|%d|size|%d|msg|%s' %
605
613
(sent , len (record ), record ))
606
- raise Exception ("Socket cant connect: unknown error" )
614
+ raise Exception (ERROR_MSGS . SOCKET_CANT_CONNECT_UNKNOWN_ERROR )
607
615
except Exception as error :
608
616
raise DevoSenderException (error ) from error
609
617
@@ -773,7 +781,7 @@ def _from_dict(config=None, con_type=None):
773
781
address = config .get ("address" , None )
774
782
775
783
if not address :
776
- raise DevoSenderException ("No address" )
784
+ raise DevoSenderException (ERROR_MSGS . NO_ADDRESS )
777
785
778
786
if not isinstance (address , tuple ):
779
787
address = (address , int (config .get ("port" , 443 )))
0 commit comments