Skip to content

Commit

Permalink
logging format is improved
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitYadav-codes committed Jul 2, 2024
1 parent 3b94ddb commit a37a10c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
14 changes: 8 additions & 6 deletions akm_tools/validation/custom_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
from typing import Dict, List

from deepdiff import DeepDiff
import json

class IDConflictException(Exception):
def __init__(self, instances: List[Dict]):
err_msg = f"More than 2 instances with same ID ! \n{instances}\n"
err_msg = f"More than 2 instances with same ID ! \n{json.dumps(instances,indent=2)}\n"
super().__init__(err_msg)
self.message = err_msg


class BaseInstanceOverwiteException(Exception):
def __init__(self, base_instance, extended_instance):
def __init__(self, base_instance: dict, extended_instance: dict):
diff = DeepDiff(base_instance, extended_instance, ignore_order=True)
err_msg = (
f"The extended instance :\n{extended_instance}\nis overwriting properties of base instance\n{base_instance}\n"
f"Issue with {base_instance['id']}\nThe extended instance is overwriting properties of base instance:\n{diff.pretty()}\n"
)
super().__init__(err_msg)
self.message = err_msg


class InvalidReferentIDException(Exception):
def __init__(self, instance, referentID):
err_msg = f"The instance :\n{instance}\nis referencing an invalid id : '{referentID}'\n"
def __init__(self, instance : dict, referentID):
err_msg = f"The instance :\n{json.dumps(instance,indent=2)}\nis referencing an invalid id : '{referentID}'\n"
super().__init__(err_msg)
self.message = err_msg
34 changes: 33 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ python = "^3.10"
PyYAML = "*"
jsonschema = "*"
black = "^24.2.0"
deepdiff = "^7.0.1"


[tool.poetry.group.dev.dependencies]
Expand Down
6 changes: 0 additions & 6 deletions tests/test_custom_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def test_IDConflictException():
instances = [{"id": 1}, {"id": 1}, {"id": 1}]
with pytest.raises(IDConflictException) as excinfo:
raise IDConflictException(instances)
assert str(excinfo.value) == f"More than 2 instances with same ID ! \n{instances}\n"


def test_BaseInstanceOverwiteException():
Expand All @@ -24,10 +23,6 @@ def test_BaseInstanceOverwiteException():
extended_instance = {"id": "data_instance2", "name": "test"}
with pytest.raises(BaseInstanceOverwiteException) as excinfo:
raise BaseInstanceOverwiteException(base_instance, extended_instance)
assert (
str(excinfo.value)
== f"The extended instance :\n{extended_instance}\nis overwriting properties of base instance\n{base_instance}\n"
)


def test_InvalidReferentIDException():
Expand All @@ -38,4 +33,3 @@ def test_InvalidReferentIDException():
referentID_value = instance["isA"]["referentID"]
with pytest.raises(InvalidReferentIDException) as excinfo:
raise InvalidReferentIDException(instance, referentID_value)
assert str(excinfo.value) == f"The instance :\n{instance}\nis referencing an invalid id : '{referentID_value}'\n"

0 comments on commit a37a10c

Please sign in to comment.