Skip to content

Commit 0275bd8

Browse files
committed
fix(collections): re-raise non-404 errors from async Collection.exists()
The async `Collection.exists()` still had a bare `except Exception: return False`, so a connection error, auth failure or timeout was indistinguishable from "collection not found". PR #1950 fixed this for the sync path only. Mirror the sync implementation: return False on 404 and re-raise anything else. Adds a mock test covering both branches on the async client. Fixes #2100
1 parent ae327ca commit 0275bd8

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

mock_tests/test_collection.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,26 @@ def test_collection_exists(weaviate_mock: HTTPServer) -> None:
484484
assert e.value.status_code == 500
485485

486486

487+
@pytest.mark.asyncio
488+
async def test_async_collection_exists(weaviate_mock: HTTPServer) -> None:
489+
non_existing = "NonExistingCollection"
490+
erroring = "ErroringCollection"
491+
weaviate_mock.expect_request(f"/v1/schema/{non_existing}").respond_with_json(
492+
response_json={"error": [{"message": "collection not found"}]}, status=404
493+
)
494+
weaviate_mock.expect_request(f"/v1/schema/{erroring}").respond_with_json(
495+
response_json={"error": [{"message": "this is an error"}]}, status=500
496+
)
497+
498+
async with weaviate.use_async_with_local(
499+
port=MOCK_PORT, host=MOCK_IP, grpc_port=MOCK_PORT_GRPC, skip_init_checks=True
500+
) as client:
501+
assert not await client.collections.use(non_existing).exists()
502+
with pytest.raises(weaviate.exceptions.UnexpectedStatusCodeError) as e:
503+
await client.collections.use(erroring).exists()
504+
assert e.value.status_code == 500
505+
506+
487507
def test_grpc_client_version_header(
488508
metadata_capture_collection: tuple[
489509
weaviate.collections.Collection, MockMetadataCaptureWeaviateService

weaviate/collections/collection/async_.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from weaviate.collections.query import _QueryCollectionAsync
2828
from weaviate.collections.tenants import _TenantsAsync
2929
from weaviate.connect.v4 import ConnectionAsync
30+
from weaviate.exceptions import UnexpectedStatusCodeError
3031
from weaviate.types import UUID
3132

3233
from .base import _CollectionBase
@@ -183,8 +184,10 @@ async def exists(self) -> bool:
183184
try:
184185
await self.config.get(simple=True)
185186
return True
186-
except Exception:
187-
return False
187+
except UnexpectedStatusCodeError as e:
188+
if e.status_code == 404:
189+
return False
190+
raise e
188191

189192
async def shards(self) -> List[Shard]:
190193
"""Get the statuses of all the shards of this collection.

0 commit comments

Comments
 (0)