Skip to content

Commit d9cba74

Browse files
fix: Replace error messages and add to ERROR_MSGS enum (#159)
* fix: Replace error messages and add to ERROR_MSGS enum * fix: `pyopenssl` dependency bumped Co-authored-by: JuanFran <101575846+JuanFranDevo@users.noreply.github.com>
1 parent 46d5e31 commit d9cba74

File tree

5 files changed

+57
-42
lines changed

5 files changed

+57
-42
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [5.0.2] - 2023-01-04
8+
### Fixed
9+
* `pyopenssl` dependency bumped
10+
### Changed
11+
* Error messages added to enum
12+
713
## [5.0.1] - 2022-12-21
814
### Fixed
915
* Error when processing some exceptions

devo/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__description__ = 'Devo Python Library.'
22
__url__ = 'http://www.devo.com'
3-
__version__ = "5.0.1"
3+
__version__ = "5.0.2"
44
__author__ = 'Devo'
55
__author_email__ = 'support@devo.com'
66
__license__ = 'MIT'

devo/sender/data.py

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,26 @@
2424

2525
class 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\noriginal 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\noriginal 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\noriginal error: %s"
33+
CHAIN_NOT_COMPATIBLE_WITH_CERT = "Error in config, the chain: %s is not compatible with the certificate: %s\noriginal 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\noriginal error: %s"
35+
INCORRECT_ADDRESS_PORT = "Error in config, incorrect address/port: %s\noriginal 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

3449
class 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-
"\noriginal 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-
"\noriginal 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-
"\noriginal 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-
"\noriginal 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

277288
class 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)))

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ click==8.1.3
22
PyYAML==6.0
33
requests==2.27.1
44
pem==21.2.0
5-
pyopenssl==22.0.0
5+
pyopenssl==22.1.*
66
urllib3>=1.26.5
77
pytz>=2019.3
8+

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"Topic :: Software Development :: Libraries :: Python Modules",
2626
]
2727
INSTALL_REQUIRES = ['requests==2.27.1', 'click==8.1.3', 'PyYAML==6.0',
28-
'pem==21.2.0', 'pyopenssl==22.0.0', 'urllib3>=1.26.5','pytz>=2019.3']
28+
'pem==21.2.0', 'pyopenssl==22.1.*', 'urllib3>=1.26.5','pytz>=2019.3']
2929
CLI = ['devo-sender=devo.sender.scripts.sender_cli:cli',
3030
'devo-api=devo.api.scripts.client_cli:cli']
3131

0 commit comments

Comments
 (0)