Skip to content

Commit 378fd0e

Browse files
author
Juliya Smith
authored
handle windows log error from ssl connection (#231)
1 parent 86833ba commit 378fd0e

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/code42cli/logger/handlers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ class SyslogServerNetworkConnectionError(Exception):
1111
"""An error raised when the connection is disrupted during logging."""
1212

1313
def __init__(self):
14-
super().__init__("Network connection broken while sending results.")
14+
super().__init__(
15+
"The network connection broke while sending results. "
16+
"This might happen if your connection requires TLS and you are attempting "
17+
"unencrypted TCP communication."
18+
)
1519

1620

1721
class NoPrioritySysLogHandler(SysLogHandler):
@@ -85,7 +89,7 @@ def handleError(self, record):
8589
them nowhere.
8690
"""
8791
t, _, _ = sys.exc_info()
88-
if t == BrokenPipeError:
92+
if issubclass(t, ConnectionError):
8993
raise SyslogServerNetworkConnectionError()
9094
super().handleError(record)
9195

tests/logger/test_handlers.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,20 @@ def socket_mocks(mocker):
4949

5050

5151
@pytest.fixture()
52-
def broken_pipe_error(mocker):
53-
mock_exc_info = mocker.patch("code42cli.logger.handlers.sys.exc_info")
54-
mock_exc_info.return_value = (BrokenPipeError, None, None)
55-
return mock_exc_info
52+
def system_exception_info(mocker):
53+
return mocker.patch("code42cli.logger.handlers.sys.exc_info")
54+
55+
56+
@pytest.fixture()
57+
def broken_pipe_error(system_exception_info):
58+
system_exception_info.return_value = (BrokenPipeError, None, None)
59+
return system_exception_info
60+
61+
62+
@pytest.fixture()
63+
def connection_reset_error(system_exception_info):
64+
system_exception_info.return_value = (ConnectionResetError, None, None)
65+
return system_exception_info
5666

5767

5868
def _get_normal_socket_initializer_mocks(mocker, new_socket):
@@ -204,7 +214,7 @@ def test_emit_when_udp_calls_socket_sendto_with_expected_message_and_address(
204214
expected_message, (_TEST_HOST, _TEST_PORT)
205215
)
206216

207-
def test_handle_error_raises_expected_error(
217+
def test_handle_error_when_broken_pipe_error_occurs_raises_expected_error(
208218
self, mock_file_event_log_record, broken_pipe_error
209219
):
210220
handler = NoPrioritySysLogHandler(
@@ -213,6 +223,15 @@ def test_handle_error_raises_expected_error(
213223
with pytest.raises(SyslogServerNetworkConnectionError):
214224
handler.handleError(mock_file_event_log_record)
215225

226+
def test_handle_error_when_connection_reset_error_occurs_raises_expected_error(
227+
self, mock_file_event_log_record, connection_reset_error
228+
):
229+
handler = NoPrioritySysLogHandler(
230+
_TEST_HOST, _TEST_PORT, ServerProtocol.UDP, None
231+
)
232+
with pytest.raises(SyslogServerNetworkConnectionError):
233+
handler.handleError(mock_file_event_log_record)
234+
216235
def test_close_when_using_tls_unwraps_socket(self):
217236
handler = NoPrioritySysLogHandler(
218237
_TEST_HOST, _TEST_PORT, ServerProtocol.TLS_TCP, None

0 commit comments

Comments
 (0)