Skip to content

Commit

Permalink
Format exception response (#77)
Browse files Browse the repository at this point in the history
* testing possibilities

* format content of violations as string

* fix test

* linting

* constraint violations as string
  • Loading branch information
tilen1976 authored Dec 16, 2024
1 parent 82a1353 commit 474fd98
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
10 changes: 4 additions & 6 deletions src/dapla_metadata/variable_definitions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +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 = [
{
"field": violation.get("field", "Unknown field"),
"message": violation.get("message", "No message provided"),
}
self.detail = "".join(
f"\n{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 src/dapla_metadata/variable_definitions/vardef.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Vardef:
"""

@classmethod
@vardef_exception_handler
def create_draft(cls, draft: Draft) -> VariableDefinition:
"""Create a Draft Variable Definition."""
return VariableDefinition.from_complete_response(
Expand Down
21 changes: 10 additions & 11 deletions tests/variable_definitions/vardef_client/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,33 @@ 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'}]"
assert exc.detail == (
"\nupdateVariableDefinitionById.updateDraft.owner.team: Invalid Dapla team"
"\nupdateVariableDefinitionById.updateDraft.owner.team: 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 == (
"\nupdateVariableDefinitionById.updateDraft.owner.team: No message provided"
"\nupdateVariableDefinitionById.updateDraft.owner.team: No message provided"
)


def test_constraint_violation_empty_violations():
response_body = CONSTRAINT_VIOLATION_BODY_MISSING_VIOLATIONS
exc = VardefClientException(response_body)
assert exc.status == BAD_REQUEST_STATUS
assert str(exc) == "Status 400: []"
assert str(exc) == "Status 400: "


def test_constraint_violation_empty_field():
response_body = CONSTRAINT_VIOLATION_BODY_MISSING_FIELD
exc = VardefClientException(response_body)
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'}]"
assert exc.detail == (
"\nUnknown field: Invalid Dapla team\nUnknown field: must not be empty"
)

0 comments on commit 474fd98

Please sign in to comment.