Skip to content

Commit 75c9d82

Browse files
authored
Merge pull request #1460 from weaviate/1.28/refactor-insufficient-permissions-inheritance
Change inheritance of new exception to maintain BC
2 parents d3160f7 + ca95702 commit 75c9d82

File tree

6 files changed

+42
-192
lines changed

6 files changed

+42
-192
lines changed

mock_tests/test_collection.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
WeaviateStartUpError,
3838
BackupCanceledError,
3939
InsufficientPermissionsError,
40+
UnexpectedStatusCodeError,
4041
)
4142

4243
ACCESS_TOKEN = "HELLO!IamAnAccessToken"
@@ -54,9 +55,15 @@ def test_insufficient_permissions(
5455
port=MOCK_PORT, host=MOCK_IP, grpc_port=MOCK_PORT_GRPC, skip_init_checks=True
5556
)
5657
collection = client.collections.get("Test")
57-
with pytest.raises(InsufficientPermissionsError) as e:
58+
59+
with pytest.raises(InsufficientPermissionsError) as e1:
60+
collection.config.get()
61+
assert "this is an error" in e1.value.message
62+
63+
with pytest.raises(UnexpectedStatusCodeError) as e2:
5864
collection.config.get()
59-
assert "this is an error" in e.value.message
65+
assert e2.value.status_code == 403
66+
6067
weaviate_mock.check_assertions()
6168

6269

requirements-devel.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ mypy==1.13.0
3939
mypy-extensions==1.0.0
4040
tomli==2.2.1
4141
types-protobuf==5.28.3.20241203
42-
types-requests==2.32.0.20241016
4342
types-urllib3==1.26.25.14
4443
typing_extensions==4.12.2
4544

weaviate/connect/authentication.py

Lines changed: 0 additions & 154 deletions
This file was deleted.

weaviate/embedded.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pathlib import Path
1616
from typing import Dict, Optional, Tuple
1717

18-
import requests
18+
import httpx
1919
import validators
2020

2121
from weaviate import exceptions
@@ -88,9 +88,7 @@ def __init__(self, options: EmbeddedOptions) -> None:
8888
self._parsed_weaviate_version = version_tag
8989
self._set_download_url_from_version_tag(version_tag)
9090
elif self.options.version == "latest":
91-
response = requests.get(
92-
"https://api.github.com/repos/weaviate/weaviate/releases/latest"
93-
)
91+
response = httpx.get("https://api.github.com/repos/weaviate/weaviate/releases/latest")
9492
latest = _decode_json_response_dict(response, "get tag of latest weaviate release")
9593
assert latest is not None
9694
self._set_download_url_from_version_tag(latest["tag_name"])

weaviate/exceptions.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from json.decoder import JSONDecodeError
66
from typing import Union, Tuple
77

8+
from grpc.aio import AioRpcError # type: ignore
89
import httpx
9-
import requests
1010

1111
ERROR_CODE_EXPLANATION = {
1212
413: """Payload Too Large. Try to decrease the batch size or increase the maximum request size on your weaviate
@@ -40,7 +40,7 @@ class UnexpectedStatusCodeError(WeaviateBaseError):
4040
not handled in the client implementation and suggests an error.
4141
"""
4242

43-
def __init__(self, message: str, response: Union[httpx.Response, requests.Response]):
43+
def __init__(self, message: str, response: Union[httpx.Response, AioRpcError]):
4444
"""
4545
Is raised in case the status code returned from Weaviate is
4646
not handled in the client implementation and suggests an error.
@@ -55,21 +55,27 @@ def __init__(self, message: str, response: Union[httpx.Response, requests.Respon
5555
`response`:
5656
The request response of which the status code was unexpected.
5757
"""
58-
self._status_code: int = response.status_code
59-
# Set error message
60-
61-
try:
62-
body = response.json()
63-
except (requests.exceptions.JSONDecodeError, httpx.DecodingError, JSONDecodeError):
64-
body = None
65-
66-
msg = (
67-
message
68-
+ f"! Unexpected status code: {response.status_code}, with response body: {body}."
69-
)
70-
if response.status_code in ERROR_CODE_EXPLANATION:
71-
msg += " " + ERROR_CODE_EXPLANATION[response.status_code]
72-
58+
if isinstance(response, httpx.Response):
59+
self._status_code: int = response.status_code
60+
# Set error message
61+
62+
try:
63+
body = response.json()
64+
except (httpx.DecodingError, JSONDecodeError):
65+
body = None
66+
67+
msg = (
68+
message
69+
+ f"! Unexpected status code: {response.status_code}, with response body: {body}."
70+
)
71+
if response.status_code in ERROR_CODE_EXPLANATION:
72+
msg += " " + ERROR_CODE_EXPLANATION[response.status_code]
73+
elif isinstance(response, AioRpcError):
74+
self._status_code = int(response.code().value[0])
75+
msg = (
76+
message
77+
+ f"! Unexpected status code: {response.code().value[1]}, with response body: {response.details()}."
78+
)
7379
super().__init__(msg)
7480

7581
@property
@@ -81,7 +87,7 @@ def status_code(self) -> int:
8187

8288

8389
class ResponseCannotBeDecodedError(WeaviateBaseError):
84-
def __init__(self, location: str, response: Union[httpx.Response, requests.Response]):
90+
def __init__(self, location: str, response: httpx.Response):
8591
"""Raised when a weaviate response cannot be decoded to json
8692
8793
Arguments:
@@ -360,10 +366,8 @@ def __init__(self, message: str, count: int) -> None:
360366
super().__init__(msg)
361367

362368

363-
class InsufficientPermissionsError(WeaviateBaseError):
369+
class InsufficientPermissionsError(UnexpectedStatusCodeError):
364370
"""Is raised when a request to Weaviate fails due to insufficient permissions."""
365371

366-
def __init__(self, res: httpx.Response) -> None:
367-
err = res.json()["error"][0]["message"]
368-
msg = f"""The request to Weaviate failed due to insufficient permissions. Details: {err}"""
369-
super().__init__(msg)
372+
def __init__(self, res: Union[httpx.Response, AioRpcError]) -> None:
373+
super().__init__("forbidden", res)

weaviate/util.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
from typing import Union, Sequence, Any, Optional, List, Dict, Generator, Tuple, cast
1313

1414
import httpx
15-
import requests
1615
import validators
17-
from requests.exceptions import JSONDecodeError
1816

1917
from weaviate.exceptions import (
2018
SchemaValidationError,
@@ -817,24 +815,22 @@ def _to_beacons(uuids: UUIDS, to_class: str = "") -> List[Dict[str, str]]:
817815
return [{"beacon": f"weaviate://localhost/{to_class}{uuid_to}"} for uuid_to in uuids]
818816

819817

820-
def _decode_json_response_dict(
821-
response: Union[httpx.Response, requests.Response], location: str
822-
) -> Optional[Dict[str, Any]]:
818+
def _decode_json_response_dict(response: httpx.Response, location: str) -> Optional[Dict[str, Any]]:
823819
if response is None:
824820
return None
825821

826822
if 200 <= response.status_code < 300:
827823
try:
828824
json_response = cast(Dict[str, Any], response.json())
829825
return json_response
830-
except JSONDecodeError:
826+
except httpx.DecodingError:
831827
raise ResponseCannotBeDecodedError(location, response)
832828

833829
raise UnexpectedStatusCodeError(location, response)
834830

835831

836832
def _decode_json_response_list(
837-
response: Union[httpx.Response, requests.Response], location: str
833+
response: httpx.Response, location: str
838834
) -> Optional[List[Dict[str, Any]]]:
839835
if response is None:
840836
return None
@@ -843,7 +839,7 @@ def _decode_json_response_list(
843839
try:
844840
json_response = response.json()
845841
return cast(list, json_response)
846-
except JSONDecodeError:
842+
except httpx.DecodingError:
847843
raise ResponseCannotBeDecodedError(location, response)
848844
raise UnexpectedStatusCodeError(location, response)
849845

0 commit comments

Comments
 (0)