From 474fd98645028377e5bff1a00a76eaf433eecad0 Mon Sep 17 00:00:00 2001 From: Cecilie Seim <68303562+tilen1976@users.noreply.github.com> Date: Mon, 16 Dec 2024 09:12:48 +0100 Subject: [PATCH] Format exception response (#77) * testing possibilities * format content of violations as string * fix test * linting * constraint violations as string --- .../variable_definitions/exceptions.py | 10 ++++----- .../variable_definitions/vardef.py | 1 + .../vardef_client/test_exceptions.py | 21 +++++++++---------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/dapla_metadata/variable_definitions/exceptions.py b/src/dapla_metadata/variable_definitions/exceptions.py index 1f9039f..dbbcbd9 100644 --- a/src/dapla_metadata/variable_definitions/exceptions.py +++ b/src/dapla_metadata/variable_definitions/exceptions.py @@ -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 diff --git a/src/dapla_metadata/variable_definitions/vardef.py b/src/dapla_metadata/variable_definitions/vardef.py index 94c02ff..cc12fd5 100644 --- a/src/dapla_metadata/variable_definitions/vardef.py +++ b/src/dapla_metadata/variable_definitions/vardef.py @@ -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( diff --git a/tests/variable_definitions/vardef_client/test_exceptions.py b/tests/variable_definitions/vardef_client/test_exceptions.py index 1f81928..b41c4a9 100644 --- a/tests/variable_definitions/vardef_client/test_exceptions.py +++ b/tests/variable_definitions/vardef_client/test_exceptions.py @@ -50,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'}]" + assert exc.detail == ( + "\nupdateVariableDefinitionById.updateDraft.owner.team: Invalid Dapla team" + "\nupdateVariableDefinitionById.updateDraft.owner.team: must not be empty" ) @@ -62,22 +60,23 @@ 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" )