Skip to content

Commit fe7ac48

Browse files
author
Juliya Smith
authored
prefix error (#232)
1 parent 9d75cc0 commit fe7ac48

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

src/code42cli/cmds/search/extraction.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ def handle_error(exception):
5858
else:
5959
message = exception
6060
logger.log_error(message)
61-
secho(str(message), err=True, fg="red")
61+
62+
message = str(message)
63+
if not message.lower().startswith("error:"):
64+
message = f"Error: {message}"
65+
66+
secho(message, err=True, fg="red")
6267

6368
handlers.handle_error = handle_error
6469
if cursor_store:

tests/cmds/test_alerts.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23

34
import py42.sdk.queries.alerts.filters as f
45
import pytest
@@ -8,6 +9,7 @@
89
from tests.cmds.conftest import get_mark_for_search_and_send_to
910
from tests.conftest import get_test_date_str
1011

12+
from code42cli import errors
1113
from code42cli import PRODUCT_NAME
1214
from code42cli.cmds.search import extraction
1315
from code42cli.cmds.search.cursor_store import AlertCursorStore
@@ -703,6 +705,21 @@ def test_search_and_send_to_with_or_query_flag_produces_expected_query(
703705
assert actual_query == expected_query
704706

705707

708+
@search_and_send_to_test
709+
def test_search_and_send_to_when_extraction_handles_error_expected_message_logged_and_printed_and_global_errored_flag_set(
710+
runner, cli_state, caplog, command
711+
):
712+
errors.ERRORED = False
713+
exception_msg = "Test Exception"
714+
cli_state.sdk.alerts.search.side_effect = Exception(exception_msg)
715+
with caplog.at_level(logging.ERROR):
716+
result = runner.invoke(cli, [*command, "--begin", "1d"], obj=cli_state)
717+
assert "Error:" in result.output
718+
assert exception_msg in result.output
719+
assert exception_msg in caplog.text
720+
assert errors.ERRORED
721+
722+
706723
@pytest.mark.parametrize(
707724
"protocol", (ServerProtocol.TLS_TCP, ServerProtocol.TLS_TCP, ServerProtocol.UDP)
708725
)

tests/cmds/test_auditlogs.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@
6161
"timestamp": TEST_AUDIT_LOG_TIMESTAMP_3,
6262
},
6363
]
64-
TEST_CHECKPOINT_EVENT_HASHLIST = [
65-
hash_event(event) for event in TEST_EVENTS_WITH_SAME_TIMESTAMP
66-
]
6764
search_and_send_to_test = get_mark_for_search_and_send_to("audit-logs")
6865

6966

tests/cmds/test_securitydata.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -773,13 +773,10 @@ def test_search_and_send_to_when_extraction_handles_error_expected_message_logge
773773
):
774774
errors.ERRORED = False
775775
exception_msg = "Test Exception"
776-
777-
def file_search_error(x):
778-
raise Exception(exception_msg)
779-
780-
cli_state.sdk.securitydata.search_file_events.side_effect = file_search_error
776+
cli_state.sdk.securitydata.search_file_events.side_effect = Exception(exception_msg)
781777
with caplog.at_level(logging.ERROR):
782778
result = runner.invoke(cli, [*command, "--begin", "1d"], obj=cli_state)
779+
assert "Error:" in result.output
783780
assert exception_msg in result.output
784781
assert exception_msg in caplog.text
785782
assert errors.ERRORED

0 commit comments

Comments
 (0)