From 65bb29ecd828ceae319bac919c6e9df3858b30e4 Mon Sep 17 00:00:00 2001 From: Kazgatari <56477241+Kazgatari@users.noreply.github.com> Date: Mon, 29 Jun 2026 13:08:36 +0200 Subject: [PATCH] Added new exception and adjusted all relevant exceptions to use the new global exception instead of own httpx --- service/src/main/inse/company_connections/logic.py | 7 ++++--- service/src/main/inse/company_connections/search_client.py | 3 +-- service/src/main/inse/exceptions.py | 5 +++++ service/src/main/inse/funders/elastic_search_client.py | 3 +-- service/src/main/inse/funders/logic.py | 2 +- service/src/main/inse/integrations/elasticsearch.py | 5 +++-- .../main/inse/patents/elastic_search_all_patents_client.py | 2 +- service/src/main/inse/patents/elastic_search_client.py | 3 +-- service/src/main/inse/patents/ipc_search_client.py | 2 +- service/src/main/inse/patents/logic.py | 2 +- service/src/main/inse/works/logic.py | 6 +++--- service/src/main/inse/works/search_client.py | 3 +-- service/src/main/inse/works/test_search_client.py | 2 +- 13 files changed, 24 insertions(+), 21 deletions(-) diff --git a/service/src/main/inse/company_connections/logic.py b/service/src/main/inse/company_connections/logic.py index 1ffea295..ad71847e 100644 --- a/service/src/main/inse/company_connections/logic.py +++ b/service/src/main/inse/company_connections/logic.py @@ -6,7 +6,8 @@ from fastapi import HTTPException from starlette.status import HTTP_502_BAD_GATEWAY, HTTP_503_SERVICE_UNAVAILABLE from . import model -from .search_client import CompanyConnectionsSearchClient, MissingApiKeyError +from inse.exceptions import MissingApiKeyError, ElasticsearchException +from .search_client import CompanyConnectionsSearchClient logger = logging.getLogger(__name__) @@ -30,7 +31,7 @@ async def get_company_connections_by_author_id( status_code=HTTP_503_SERVICE_UNAVAILABLE, detail="Elasticsearch is not configured.", ) from exc - except httpx.HTTPError as exc: + except ElasticsearchException as exc: logger.exception("OpenAlex author lookup failed for %s.", author_id) raise HTTPException( status_code=HTTP_502_BAD_GATEWAY, @@ -68,7 +69,7 @@ async def get_company_connections_by_author_id( status_code=HTTP_503_SERVICE_UNAVAILABLE, detail="Elasticsearch is not configured.", ) from exc - except httpx.HTTPError as exc: + except ElasticsearchException as exc: logger.exception("Handelsregister lookup failed for %s.", display_name) raise HTTPException( status_code=HTTP_502_BAD_GATEWAY, diff --git a/service/src/main/inse/company_connections/search_client.py b/service/src/main/inse/company_connections/search_client.py index ddc775c7..6f041c50 100644 --- a/service/src/main/inse/company_connections/search_client.py +++ b/service/src/main/inse/company_connections/search_client.py @@ -10,8 +10,7 @@ from inse.model import ESCluster from inse.exceptions import ElasticsearchNotFoundException -class MissingApiKeyError(Exception): - pass +from inse.exceptions import MissingApiKeyError class CompanyConnectionsSearchClient: diff --git a/service/src/main/inse/exceptions.py b/service/src/main/inse/exceptions.py index ed59f58b..f7331b83 100644 --- a/service/src/main/inse/exceptions.py +++ b/service/src/main/inse/exceptions.py @@ -16,3 +16,8 @@ class ElasticsearchNotFoundException(NotFoundException, ElasticsearchException): class ElasticsearchConnectionException(ElasticsearchException): """Raised when connecting to Elasticsearch fails.""" pass + + +class MissingApiKeyError(Exception): + """Raised when a required API key is missing.""" + pass diff --git a/service/src/main/inse/funders/elastic_search_client.py b/service/src/main/inse/funders/elastic_search_client.py index 22029054..08dc6bde 100644 --- a/service/src/main/inse/funders/elastic_search_client.py +++ b/service/src/main/inse/funders/elastic_search_client.py @@ -12,8 +12,7 @@ from inse.integrations import elasticsearch from inse.model import ESCluster -class MissingApiKeyError(Exception): - pass +from inse.exceptions import MissingApiKeyError class FundedPublicationsSearchClient: diff --git a/service/src/main/inse/funders/logic.py b/service/src/main/inse/funders/logic.py index 653d1bd9..098116e9 100644 --- a/service/src/main/inse/funders/logic.py +++ b/service/src/main/inse/funders/logic.py @@ -15,7 +15,7 @@ from starlette.status import HTTP_503_SERVICE_UNAVAILABLE from ..persons import logic as person_logic from . import model from .elastic_search_client import FundedPublicationsSearchClient -from .elastic_search_client import MissingApiKeyError +from inse.exceptions import MissingApiKeyError logger = logging.getLogger(__name__) diff --git a/service/src/main/inse/integrations/elasticsearch.py b/service/src/main/inse/integrations/elasticsearch.py index 1cb37add..803aa212 100644 --- a/service/src/main/inse/integrations/elasticsearch.py +++ b/service/src/main/inse/integrations/elasticsearch.py @@ -8,6 +8,7 @@ from inse.exceptions import ( ElasticsearchException, ElasticsearchNotFoundException, ElasticsearchConnectionException, + MissingApiKeyError, ) from inse.model import ESCluster, ESIndex from .. import config @@ -50,7 +51,7 @@ def _cluster_credentials(cluster: ESCluster) -> Tuple[str, str]: not config.settings.elasticsearch_url or not config.settings.elasticsearch_api_key ): - raise ValueError( + raise MissingApiKeyError( "Elasticsearch URL and API key must be set in the configuration for cluster 'nes'" ) @@ -60,7 +61,7 @@ def _cluster_credentials(cluster: ESCluster) -> Tuple[str, str]: not config.settings.elasticsearch_inse_url or not config.settings.elasticsearch_inse_api_key ): - raise ValueError( + raise MissingApiKeyError( "Elasticsearch INSE URL and API key must be set in the configuration for cluster 'inse'" ) diff --git a/service/src/main/inse/patents/elastic_search_all_patents_client.py b/service/src/main/inse/patents/elastic_search_all_patents_client.py index da4c959c..bdca70f5 100644 --- a/service/src/main/inse/patents/elastic_search_all_patents_client.py +++ b/service/src/main/inse/patents/elastic_search_all_patents_client.py @@ -8,7 +8,7 @@ from typing import Any import httpx from .. import config -from .elastic_search_client import MissingApiKeyError +from inse.exceptions import MissingApiKeyError from inse.integrations import elasticsearch from inse.model import ESCluster diff --git a/service/src/main/inse/patents/elastic_search_client.py b/service/src/main/inse/patents/elastic_search_client.py index 803201aa..a7860f4d 100644 --- a/service/src/main/inse/patents/elastic_search_client.py +++ b/service/src/main/inse/patents/elastic_search_client.py @@ -27,8 +27,7 @@ def _match_name_field(field: str, name: str) -> dict[str, Any]: } -class MissingApiKeyError(Exception): - pass +from inse.exceptions import MissingApiKeyError class PatentSearchClient: diff --git a/service/src/main/inse/patents/ipc_search_client.py b/service/src/main/inse/patents/ipc_search_client.py index 6e952e19..834b82af 100644 --- a/service/src/main/inse/patents/ipc_search_client.py +++ b/service/src/main/inse/patents/ipc_search_client.py @@ -11,7 +11,7 @@ import httpx from .. import config from . import model -from .elastic_search_client import MissingApiKeyError +from inse.exceptions import MissingApiKeyError from .ipc_lookup import IpcHierarchyKeys from .ipc_lookup import build_ipc_classification from .ipc_lookup import parse_ipc_symbol diff --git a/service/src/main/inse/patents/logic.py b/service/src/main/inse/patents/logic.py index 3142ac3c..fcf2e446 100644 --- a/service/src/main/inse/patents/logic.py +++ b/service/src/main/inse/patents/logic.py @@ -15,7 +15,7 @@ from starlette.status import HTTP_404_NOT_FOUND from ..persons import logic as person_logic from ..models.openalex.author import Author from . import model -from .elastic_search_client import MissingApiKeyError +from inse.exceptions import MissingApiKeyError from .elastic_search_client import PatentSearchClient from .elastic_search_all_patents_client import AllPatentsSearchClient from .ipc_search_client import IpcSearchClient diff --git a/service/src/main/inse/works/logic.py b/service/src/main/inse/works/logic.py index 0d561b37..81d5dbd5 100644 --- a/service/src/main/inse/works/logic.py +++ b/service/src/main/inse/works/logic.py @@ -24,7 +24,7 @@ from inse.models.openalex.meta import Meta from inse.models.openalex.topic_count import TopicCount from inse.models.openalex.topics_by_year import TopicsByYear from inse.models.openalex.topics_by_time import TopicsByTime -from .search_client import MissingApiKeyError +from inse.exceptions import MissingApiKeyError from .search_client import OpenAlexWorksSearchClient logger = logging.getLogger(__name__) @@ -122,10 +122,10 @@ async def get_primary_topic_counts( author_id: str, client: OpenAlexWorksSearchClient | None = None, ) -> List[TopicCount]: - topic_counts = _get_primary_topic_counts_from_es(author_id) + topic_counts = await _get_primary_topic_counts_from_es(author_id) topic_ids = [item["id"] for item in topic_counts if item["id"]] - details = _get_topic_details(topic_ids) + details = await _get_topic_details(topic_ids) result = [] diff --git a/service/src/main/inse/works/search_client.py b/service/src/main/inse/works/search_client.py index d4f0aa40..41e6cb1e 100644 --- a/service/src/main/inse/works/search_client.py +++ b/service/src/main/inse/works/search_client.py @@ -11,8 +11,7 @@ import urllib from .. import config from inse.integrations import elasticsearch from inse.model import ESCluster, ESIndex -class MissingApiKeyError(Exception): - pass +from inse.exceptions import MissingApiKeyError logger = logging.getLogger(__name__) diff --git a/service/src/main/inse/works/test_search_client.py b/service/src/main/inse/works/test_search_client.py index 1297d848..39dbf091 100644 --- a/service/src/main/inse/works/test_search_client.py +++ b/service/src/main/inse/works/test_search_client.py @@ -3,7 +3,7 @@ import json import httpx import pytest -from inse.works.search_client import MissingApiKeyError +from inse.exceptions import MissingApiKeyError from inse.works.search_client import OpenAlexWorksSearchClient