Skip to content

Commit

Permalink
Add error message property (#190)
Browse files Browse the repository at this point in the history
* Add error message property to api response class

* add more clarity to server_message func

* add more clarity to server_message func 2
  • Loading branch information
philkra authored Jan 3, 2024
1 parent f608371 commit 62e3797
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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
14 changes: 13 additions & 1 deletion xata/api_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def __init__(self, response: Response):

def server_message(self) -> Union[str, None]:
"""
Get the server message from the response
Get the server message from the response, if you need the error message
please the property error_message. This channel is only relevant for
deprecation messages or other meta information from Xata.
:returns str | None
"""
return self.headers["x-xata-message"] if "x-xata-message" in self.headers else None
Expand Down Expand Up @@ -93,6 +95,16 @@ def status_code(self) -> int:
:returns int
"""
return self.response.status_code

@property
def error_message(self) -> Union[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 62e3797

Please sign in to comment.