Skip to content

Commit

Permalink
format violations inline to add default messages - remove formatting …
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
tilen1976 committed Dec 12, 2024
1 parent 9f22080 commit 2bd4547
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
26 changes: 7 additions & 19 deletions src/dapla_metadata/variable_definitions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ def __init__(self, response_body: str) -> None:
the detail is a list with field and message.
response_body (str): The raw response body string, stored for
debugging purposes.
"""
self.detail: str | list
try:
data = json.loads(response_body)
self.status = data.get("status", "Unknown status")
if data.get("title") == "Constraint Violation":
violations = data.get("violations", [])
self.detail = self._format_violations(violations)
self.detail = [
{
"field": violation.get("field", "Unknown field"),
"message": violation.get("message", "No message provided"),
}
for violation in violations
]
else:
self.detail = data.get("detail", "No detail provided")
self.response_body = response_body
Expand All @@ -50,23 +55,6 @@ def __init__(self, response_body: str) -> None:
data = None
super().__init__(f"Status {self.status}: {self.detail}")

def _format_violations(self, violations: list) -> list:
"""Format a list of violations into a readable string.
Args:
violations (list): List of violation dictionaries with 'field' and 'message'.
Returns:
str: Formatted string of violations.
"""
return [
{
"Field": violation.get("field", "Unknown field"),
"Message": violation.get("message", "No message provided"),
}
for violation in violations
]


def vardef_exception_handler(method): # noqa: ANN201, ANN001
"""Decorator for handling exceptions in Vardef."""
Expand Down
16 changes: 8 additions & 8 deletions tests/variable_definitions/vardef_client/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test_invalid_json():
response_body = "Not a JSON string"
exc = VardefClientException(response_body)
assert exc.status == "Unknown"
assert exc.detail == "Invalid response body"
assert str(exc) == "Status Unknown: Invalid response body"
assert exc.detail == "Could not decode error response from API"
assert str(exc) == "Status Unknown: Could not decode error response from API"


def test_missing_keys():
Expand All @@ -50,19 +50,19 @@ def test_constraint_violation():
response_body = CONSTRAINT_VIOLATION_BODY
exc = VardefClientException(response_body)
assert exc.status == BAD_REQUEST_STATUS
assert exc.detail[0]["Message"] == "Invalid Dapla team"
assert exc.detail[0]["message"] == "Invalid Dapla team"
assert (
str(exc) == "Status 400: ["
"{'Field': 'updateVariableDefinitionById.updateDraft.owner.team', 'Message': 'Invalid Dapla team'}, "
"{'Field': 'updateVariableDefinitionById.updateDraft.owner.team', 'Message': 'must not be empty'}]"
"{'field': 'updateVariableDefinitionById.updateDraft.owner.team', 'message': 'Invalid Dapla team'}, "
"{'field': 'updateVariableDefinitionById.updateDraft.owner.team', 'message': 'must not be empty'}]"
)


def test_constraint_violation_missing_messages():
response_body = CONSTRAINT_VIOLATION_BODY_MISSING_MESSAGES
exc = VardefClientException(response_body)
assert exc.status == BAD_REQUEST_STATUS
assert exc.detail[0]["Message"] == "No message provided"
assert exc.detail[0]["message"] == "No message provided"


def test_constraint_violation_empty_violations():
Expand All @@ -78,6 +78,6 @@ def test_constraint_violation_empty_field():
assert exc.status == BAD_REQUEST_STATUS
assert str(exc) == (
"Status 400: ["
"{'Field': 'Unknown field', 'Message': 'Invalid Dapla team'}, "
"{'Field': 'Unknown field', 'Message': 'must not be empty'}]"
"{'field': 'Unknown field', 'message': 'Invalid Dapla team'}, "
"{'field': 'Unknown field', 'message': 'must not be empty'}]"
)

0 comments on commit 2bd4547

Please sign in to comment.