diff --git a/tests/integration-tests/api_response_test.py b/tests/integration-tests/api_response_test.py index 2d87969..f6eb969 100644 --- a/tests/integration-tests/api_response_test.py +++ b/tests/integration-tests/api_response_test.py @@ -84,3 +84,16 @@ def test_get_cursor_and_has_more_results(self): assert posts.is_success() assert len(posts["records"]) == 1 assert not posts.has_more_results() + + def test_error_message(self): + user = self.client.records().get("Nope", "nope^2") + assert not user.is_success() + assert user.status_code > 299 + assert user.error_message is not None + assert user.error_message.endswith("not found") + + def test_error_message_should_not_be_set(self): + user = self.client.users().get() + assert user.is_success() + assert user.status_code < 300 + assert not user.error_message diff --git a/xata/api_response.py b/xata/api_response.py index 7b88aff..3f7f6ba 100644 --- a/xata/api_response.py +++ b/xata/api_response.py @@ -93,6 +93,16 @@ def status_code(self) -> int: :returns int """ return self.response.status_code + + @property + def error_message(self) -> str | None: + """ + Get the error message if it is set, otherwise None + :returns str | None + """ + if self.status_code < 300: + return None + return self.response.json().get("message", None) @property def headers(self) -> dict: