55from json .decoder import JSONDecodeError
66from typing import Union , Tuple
77
8+ from grpc .aio import AioRpcError # type: ignore
89import httpx
9- import requests
1010
1111ERROR_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
8389class 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 )
0 commit comments