Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CCXDEV-11808] Remove all the f-strings from the exceptions #126

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ccx_messaging/consumers/kafka_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def get_url(self, input_msg: dict) -> str:
# The `handles` method should prevent this from
# being called if the input message format is wrong.
except Exception as ex:
raise CCXMessagingError(f"Unable to extract URL from input message: {ex}") from ex
LOG.error("Unable to extract URL from input message: %s", ex)
raise CCXMessagingError("Unable to extract URL from input message") from ex

def run(self):
"""Consume message and proccess."""
Expand Down
11 changes: 6 additions & 5 deletions ccx_messaging/downloaders/http_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,21 @@ def get(self, src):
"""Download a file from HTTP server and store it in a temporary file."""
if not self.allow_unsafe_links:
if src is None or not HTTPDownloader.HTTP_RE.fullmatch(src):
raise CCXMessagingError(f"Invalid URL format: {src}")
LOG.error("Invalid URL format: %s", src)
raise CCXMessagingError("Invalid URL format")

try:
response = requests.get(src)
data = response.content
size = len(data)

if size == 0:
raise CCXMessagingError(f"Empty input archive from {src}")
LOG.error("Empty input archive from: %s", src)
raise CCXMessagingError("Empty input archive")

if self.max_archive_size is not None and size > self.max_archive_size:
raise CCXMessagingError(
f"The archive is too big ({size} > {self.max_archive_size}). Skipping"
)
LOG.error("The archive is too big ({size} > {self.max_archive_size})", size=size)
raise CCXMessagingError("The archive is too big. Skipping")

with NamedTemporaryFile() as file_data:
file_data.write(data)
Expand Down
3 changes: 0 additions & 3 deletions ccx_messaging/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,5 @@ def format(self, input_msg):
return (
f"Status: Error; "
f"Topic: {input_msg['topic']}; "
f"Partition: {input_msg['partition']}; "
f"Offset: {input_msg['offset']}; "
f"Cluster: {input_msg['cluster_name']}; "
f"Cause: {self}"
)
25 changes: 14 additions & 11 deletions ccx_messaging/ingress.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@ def parse_identity(encoded_identity: bytes) -> dict:
return identity

except TypeError as ex:
raise CCXMessagingError("Bad argument type %s", encoded_identity) from ex
LOG.error("Bad argument type %s", encoded_identity)
raise CCXMessagingError("Bad argument type") from ex

except binascii.Error as ex:
raise CCXMessagingError(
f"Base64 encoded identity could not be parsed: {encoded_identity}"
) from ex
LOG.error("Base64 encoded identity could not be parsed: %s", encoded_identity)
raise CCXMessagingError("Base64 encoded identity could not be parsed") from ex

except json.JSONDecodeError as ex:
raise CCXMessagingError(f"Unable to decode received message: {decoded_identity}") from ex
LOG.error("Unable to decode received message: %s", decoded_identity)
raise CCXMessagingError("Unable to decode received message") from ex

except jsonschema.ValidationError as ex:
raise CCXMessagingError(f"Invalid input message JSON schema: {identity}") from ex
LOG.error("Invalid input message JSON schema: %s", identity)
raise CCXMessagingError("Invalid input message JSON schema") from ex


def parse_ingress_message(message: bytes) -> dict:
Expand All @@ -45,15 +47,16 @@ def parse_ingress_message(message: bytes) -> dict:
jsonschema.validate(instance=deserialized_message, schema=INPUT_MESSAGE_SCHEMA)

except TypeError as ex:
raise CCXMessagingError(f"Incorrect message type: {message}") from ex
LOG.error("Incorrect message type: %s", message)
raise CCXMessagingError("Incorrect message type") from ex

except json.JSONDecodeError as ex:
raise CCXMessagingError(f"Unable to decode received message: {message}") from ex
LOG.error("Unable to decode received message: %s", message)
raise CCXMessagingError("Unable to decode received message") from ex

except jsonschema.ValidationError as ex:
raise CCXMessagingError(
f"Invalid input message JSON schema: {deserialized_message}"
) from ex
LOG.error("Invalid input message JSON schema: %s", deserialized_message)
raise CCXMessagingError("Invalid input message JSON schema") from ex

LOG.debug("JSON schema validated: %s", deserialized_message)

Expand Down
11 changes: 6 additions & 5 deletions ccx_messaging/publishers/dvo_metrics_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,25 @@ def publish(self, input_msg: Dict, report: str) -> None:

try:
report = json.loads(report)
except (TypeError, json.decoder.JSONDecodeError):
raise CCXMessagingError("Could not parse report; report is not in JSON format")
except (TypeError, json.decoder.JSONDecodeError) as err:
raise CCXMessagingError("Could not parse report; report is not in JSON format") from err

report.pop("reports", None)

try:
org_id = int(input_msg["identity"]["identity"]["internal"]["org_id"])
except (ValueError, KeyError, TypeError) as err:
raise CCXMessagingError(f"Error extracting the OrgID: {err}") from err
log.error("Error extracting the OrgID: %s", err)
raise CCXMessagingError("Error extracting the OrgID") from err

try:
account_number = int(input_msg["identity"]["identity"]["account_number"])
except (ValueError, KeyError, TypeError) as err:
log.warning(f"Error extracting the Account number: {err}")
log.warning("Error extracting the Account number: %s", err)
account_number = ""

if "cluster_name" not in input_msg:
raise CCXMessagingError()
raise CCXMessagingError("Can't find 'cluster_name'")

output_msg = {
"OrgID": org_id,
Expand Down
9 changes: 5 additions & 4 deletions ccx_messaging/publishers/rule_processing_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ def publish(self, input_msg, report):
try:
org_id = int(input_msg["identity"]["identity"]["internal"]["org_id"])
except (ValueError, KeyError, TypeError) as err:
raise CCXMessagingError(f"Error extracting the OrgID: {err}") from err
log.error("Error extracting the OrgID: %s", err)
raise CCXMessagingError("Error extracting the OrgID") from err

try:
account_number = int(input_msg["identity"]["identity"]["account_number"])
except (ValueError, KeyError, TypeError) as err:
log.warning(f"Error extracting the Account number: {err}")
log.warning("Error extracting the Account number: %s", err)
account_number = ""

try:
Expand Down Expand Up @@ -148,5 +149,5 @@ def publish(self, input_msg, report):
raise CCXMessagingError("Missing expected keys in the input message") from err

except (TypeError, UnicodeEncodeError, JSONDecodeError) as err:
log.info(err)
raise CCXMessagingError(f"Error encoding the response to publish: {report}") from err
log.error(err)
raise CCXMessagingError("Error encoding the response to publish") from err
8 changes: 5 additions & 3 deletions ccx_messaging/publishers/workloads_info_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ def publish(self, input_msg: dict, response: str):
try:
org_id = int(input_msg["identity"]["identity"]["internal"]["org_id"])
except (ValueError, TypeError, KeyError) as err:
raise CCXMessagingError(f"Error extracting the OrgID: {err}") from err
log.error("Error extracting the OrgID: %s", err)
raise CCXMessagingError("Error extracting the OrgID") from err

try:
account_number = int(input_msg["identity"]["identity"]["account_number"])
except (ValueError, KeyError, TypeError) as err:
log.warning(f"Error extracting the Account number: {err}")
log.warning("Error extracting the Account number: %s", err)
account_number = ""

# outgoing message in form of JSON
Expand Down Expand Up @@ -116,4 +117,5 @@ def publish(self, input_msg: dict, response: str):
)

except (KeyError, TypeError, UnicodeEncodeError, JSONDecodeError) as err:
raise CCXMessagingError(f"Error encoding the response to publish: {message}") from err
log.error("Error encoding the response to publish: %s", message)
raise CCXMessagingError("Error encoding the response to publish") from err
18 changes: 16 additions & 2 deletions test/downloaders/http_downloader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,28 @@

import pytest

from ccx_messaging.downloaders.http_downloader import HTTPDownloader
from ccx_messaging.downloaders.http_downloader import HTTPDownloader, parse_human_input
from ccx_messaging.error import CCXMessagingError


_INVALID_FILE_SIZES = [42, 2.71, True, [], {}, "1234J"]
_VALID_FILE_SIZES = ["1K", "2M", "3G", "4T", "5Ki", "6Mi", "7Gi", "8Ti"]

@pytest.mark.parametrize("file_size", _INVALID_FILE_SIZES)
def test_parse_human_input_invalid_file_sizes(file_size):
"""Test that passing invalid data to parse_human_input raise an exception."""
with pytest.raises((TypeError, ValueError)):
parse_human_input(file_size)

@pytest.mark.parametrize("file_size", _VALID_FILE_SIZES)
def test_parse_human_input_valid_file_sizes(file_size):
"""Test that passing valid data to parse_human_input doesn't raise an exception."""
parse_human_input(file_size)


_REGEX_BAD_URL_FORMAT = r"^Invalid URL format: .*"
_INVALID_TYPE_URLS = [42, 2.71, True, [], {}]


@pytest.mark.parametrize("url", _INVALID_TYPE_URLS)
def test_get_invalid_type(url):
"""Test that passing invalid data type to `get` raises an exception."""
Expand Down
3 changes: 1 addition & 2 deletions test/error_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def test_error_formatting():

fmt = err.format(input_msg)
expected = (
"Status: Error; Topic: topic name; Partition: partition name; "
+ "Offset: 1234; Cluster: clusterName; Cause: CCXMessagingError"
"Status: Error; Topic: topic name; Cause: CCXMessagingError"
)
assert fmt == expected
Loading