Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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