Skip to content

Commit

Permalink
format content of violations as string
Browse files Browse the repository at this point in the history
  • Loading branch information
tilen1976 committed Dec 13, 2024
1 parent fc8a379 commit 4064185
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 30 deletions.
15 changes: 5 additions & 10 deletions src/dapla_metadata/variable_definitions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,11 @@ def __init__(self, response_body: str) -> None:
self.status = data.get("status", "Unknown status")
if data.get("title") == "Constraint Violation":
violations = data.get("violations", [])
self.detail = json.dumps(
[
{
"field": violation.get("field", "Unknown field"),
"message": violation.get("message", "No message provided"),
}
for violation in violations
],
indent=4,
)
self.detail = [
f"{violation.get('field', 'Unknown field')}: {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 Down
1 change: 1 addition & 0 deletions tests/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"message": "Invalid Dapla team"
},
{
"field": "updateVariableDefinitionById.updateDraft.owner.team",
"message": "must not be empty"
}
]
Expand Down
26 changes: 6 additions & 20 deletions tests/variable_definitions/vardef_client/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Tests for Vardef client exception handling."""

import json

from dapla_metadata.variable_definitions.exceptions import VardefClientException
from tests.utils.constants import BAD_REQUEST_STATUS
from tests.utils.constants import CONSTRAINT_VIOLATION_BODY
Expand Down Expand Up @@ -52,11 +50,9 @@ 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 (
str(exc) == "Status 400: ["
"{'field': 'updateVariableDefinitionById.updateDraft.owner.team', 'message': 'Invalid Dapla team'}, "
"{'field': 'updateVariableDefinitionById.updateDraft.owner.team', 'message': 'must not be empty'}]"
exc.detail[0]
== "updateVariableDefinitionById.updateDraft.owner.team: Invalid Dapla team"
)


Expand All @@ -78,17 +74,7 @@ def test_constraint_violation_empty_field():
response_body = CONSTRAINT_VIOLATION_BODY_MISSING_FIELD
exc = VardefClientException(response_body)
assert exc.status == BAD_REQUEST_STATUS
excpected_message = json.dumps(
[
{
"field": "Unknown field",
"message": "Invalid Dapla team",
},
{
"field": "Unknown field",
"message": "must not be empty",
},
],
indent=4,
)
assert exc.detail == excpected_message
assert exc.detail == [
"Unknown field: Invalid Dapla team",
"updateVariableDefinitionById.updateDraft.owner.team: must not be empty",
]

0 comments on commit 4064185

Please sign in to comment.