diff --git a/src/dapla_metadata/variable_definitions/exceptions.py b/src/dapla_metadata/variable_definitions/exceptions.py index 7407d1e..1f9039f 100644 --- a/src/dapla_metadata/variable_definitions/exceptions.py +++ b/src/dapla_metadata/variable_definitions/exceptions.py @@ -32,7 +32,6 @@ 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: @@ -40,7 +39,13 @@ 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 = 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 @@ -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.""" diff --git a/tests/variable_definitions/vardef_client/test_exceptions.py b/tests/variable_definitions/vardef_client/test_exceptions.py index bcb6ddf..1f81928 100644 --- a/tests/variable_definitions/vardef_client/test_exceptions.py +++ b/tests/variable_definitions/vardef_client/test_exceptions.py @@ -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(): @@ -50,11 +50,11 @@ 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'}]" ) @@ -62,7 +62,7 @@ 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(): @@ -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'}]" )