Skip to content

Commit 3061651

Browse files
committed
Add test for bad save endpoint
1 parent 779326c commit 3061651

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/datalab_api/_base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ def _handle_response(
252252
try:
253253
error_data = response.json()
254254
error_message = error_data.get("message", str(response.content))
255+
error_message += f"\nFull JSON: {error_data}"
255256
except ValueError:
256257
error_message = str(response.content)
257258

@@ -273,9 +274,10 @@ def _handle_response(
273274
if hasattr(self, "_datalab_server_version"):
274275
server_info = f" (Server version: {self._datalab_server_version})"
275276
raise DatalabAPIError(
276-
f"HTTP 500 Internal Server Error at {url}: {error_message}{server_info}. "
277-
"This is a server-side bug. Please report this issue to the datalab developers "
278-
"at https://github.com/datalab-org/datalab/issues with the full error message."
277+
f"""HTTP 500 Internal Server Error at {url} {server_info}.
278+
This is likely a server-side bug. Please report this issue to the datalab developers at https://github.com/datalab-org/datalab/issues with the full error message below:
279+
280+
\n{error_message}"""
279281
)
280282
elif response.status_code == 502:
281283
raise DatalabAPIError(

tests/conftest.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,26 @@ def mocked_api(
4545
fake_item_api = respx_mock.get("/get-item-data/KUVEKJ", name="sample-KUVEKJ")
4646
fake_item_api.return_value = Response(200, json=fake_sample_json)
4747

48+
fake_missing_item_api = respx_mock.get("/get-item-data/TEST", name="sample-missing-TEST")
49+
fake_missing_item_api.return_value = Response(
50+
404,
51+
json={
52+
"message": "No matching items for match={'item_id': 'TEST'} with current authorization",
53+
"status": "error",
54+
},
55+
)
56+
4857
fake_collection_api = respx_mock.get(
4958
"/collections/test_collection", name="collection-test_collection"
5059
)
5160
fake_collection_api.return_value = Response(200, json=fake_collection_json)
5261

62+
# Fake an unhandled exception
63+
fake_internal_error = respx_mock.post("/save-item/", name="bad-save")
64+
fake_internal_error.return_value = Response(
65+
500, json={"message": "('type',)", "title": "KeyError"}
66+
)
67+
5368
yield respx_mock
5469

5570

tests/test_client.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import respx
33

44
from datalab_api import DatalabClient
5+
from datalab_api._base import DatalabAPIError
56

67

78
def test_redirect_url(mocked_api, mocked_ui, fake_api_url, fake_ui_url):
@@ -17,7 +18,7 @@ def test_redirect_url(mocked_api, mocked_ui, fake_api_url, fake_ui_url):
1718
assert client.datalab_api_url == fake_api_url
1819

1920

20-
def test_sample_list(mocked_api, fake_api_url):
21+
def test_sample_operations(mocked_api, fake_api_url):
2122
with DatalabClient(fake_api_url) as client:
2223
assert mocked_api["api"].called
2324
assert mocked_api["info"].called
@@ -35,6 +36,18 @@ def test_sample_list(mocked_api, fake_api_url):
3536
assert mocked_api["sample-KUVEKJ"].called
3637
assert sample["item_id"] == "KUVEKJ"
3738

39+
# Check that the full server error message is shown for a missing item
40+
with pytest.raises(
41+
DatalabAPIError,
42+
match="No matching items for match={'item_id': 'TEST'} with current authorization",
43+
):
44+
client.get_item("TEST", display=True)
45+
assert mocked_api["sample-missing-TEST"].called
46+
47+
with pytest.raises(DatalabAPIError, match="KeyError"):
48+
client.update_item(item_id="TEST", item_data={"name": "test"})
49+
assert mocked_api["bad-save"].called
50+
3851

3952
@respx.mock
4053
def test_collection_get(fake_api_url, mocked_api):

0 commit comments

Comments
 (0)