diff --git a/src/rubrix/client/api.py b/src/rubrix/client/api.py index 5fe65bbd15..7193607cb4 100644 --- a/src/rubrix/client/api.py +++ b/src/rubrix/client/api.py @@ -149,8 +149,10 @@ def __init__( self._agent = _RubrixLogAgent(self) def __del__(self): - del self._client - del self._agent + if hasattr(self,"_client"): + del self._client + if hasattr(self,"_agent"): + del self._agent @property def client(self): diff --git a/src/rubrix/client/sdk/client.py b/src/rubrix/client/sdk/client.py index 6d529053eb..87fafa10c8 100644 --- a/src/rubrix/client/sdk/client.py +++ b/src/rubrix/client/sdk/client.py @@ -13,12 +13,14 @@ # See the License for the specific language governing permissions and # limitations under the License. import dataclasses +import functools from typing import Dict, TypeVar import httpx from rubrix._constants import API_KEY_HEADER_NAME from rubrix.client.sdk._helpers import build_raw_response +from rubrix.client.sdk.commons.errors import RubrixClientError @dataclasses.dataclass @@ -72,6 +74,18 @@ def __del__(self): def __hash__(self): return hash(self.base_url) + def with_httpx_error_handler(func): + @functools.wraps(func) + def inner(self, *args, **kwargs): + try: + result = func(self, *args, **kwargs) + return result + except httpx.ConnectError as err: + err_str = f"Your Api endpoint at {self.base_url} is not available or not responding." + raise RubrixClientError(err_str) from None + return inner + + @with_httpx_error_handler def get(self, path: str, *args, **kwargs): path = self._normalize_path(path) response = self.__httpx__.get( @@ -82,6 +96,7 @@ def get(self, path: str, *args, **kwargs): ) return build_raw_response(response).parsed + @with_httpx_error_handler def post(self, path: str, *args, **kwargs): path = self._normalize_path(path) @@ -93,6 +108,7 @@ def post(self, path: str, *args, **kwargs): ) return build_raw_response(response).parsed + @with_httpx_error_handler def put(self, path: str, *args, **kwargs): path = self._normalize_path(path) response = self.__httpx__.put( @@ -103,6 +119,7 @@ def put(self, path: str, *args, **kwargs): ) return build_raw_response(response).parsed + @with_httpx_error_handler def stream(self, path: str, *args, **kwargs): return self.__httpx__.stream( url=path, diff --git a/src/rubrix/server/server.py b/src/rubrix/server/server.py index d5f316cfc0..d561168685 100644 --- a/src/rubrix/server/server.py +++ b/src/rubrix/server/server.py @@ -27,7 +27,10 @@ from rubrix import __version__ as rubrix_version from rubrix.logging import configure_logging -from rubrix.server.daos.backend.elasticsearch import ElasticsearchBackend +from rubrix.server.daos.backend.elasticsearch import ( + ElasticsearchBackend, + GenericSearchError, +) from rubrix.server.daos.datasets import DatasetsDAO from rubrix.server.daos.records import DatasetRecordsDAO from rubrix.server.errors import APIErrorHandler, EntityNotFoundError @@ -86,8 +89,6 @@ def configure_app_statics(app: FastAPI): def configure_app_storage(app: FastAPI): @app.on_event("startup") async def configure_elasticsearch(): - import opensearchpy - try: es_wrapper = ElasticsearchBackend.get_instance() dataset_records: DatasetRecordsDAO = DatasetRecordsDAO(es_wrapper) @@ -96,7 +97,7 @@ async def configure_elasticsearch(): ) datasets.init() dataset_records.init() - except opensearchpy.exceptions.ConnectionError as error: + except GenericSearchError as error: raise ConfigError( f"Your Elasticsearch endpoint at {settings.obfuscated_elasticsearch()} " "is not available or not responding.\n" diff --git a/tests/client/sdk/users/test_api.py b/tests/client/sdk/users/test_api.py index 2c77c92fee..401fd5fd7a 100644 --- a/tests/client/sdk/users/test_api.py +++ b/tests/client/sdk/users/test_api.py @@ -3,7 +3,7 @@ from rubrix._constants import DEFAULT_API_KEY from rubrix.client.sdk.client import AuthenticatedClient -from rubrix.client.sdk.commons.errors import UnauthorizedApiError +from rubrix.client.sdk.commons.errors import RubrixClientError, UnauthorizedApiError from rubrix.client.sdk.users.api import whoami from rubrix.client.sdk.users.models import User @@ -23,7 +23,7 @@ def test_whoami_with_auth_error(monkeypatch, mocked_client): def test_whoami_with_connection_error(): - with pytest.raises(httpx.ConnectError): + with pytest.raises(RubrixClientError): whoami( AuthenticatedClient(base_url="http://localhost:6900", token="wrong-apikey") )