Skip to content

Commit

Permalink
Add error message property to api response class
Browse files Browse the repository at this point in the history
  • Loading branch information
philkra committed Jan 3, 2024
1 parent f608371 commit 0592db9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/integration-tests/api_response_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions xata/api_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0592db9

Please sign in to comment.