Added new exception and adjusted all relevant exceptions to use the new global exception instead of own httpx

This commit is contained in:
Kazgatari
2026-06-29 13:08:36 +02:00
parent aca6ae20b2
commit 65bb29ecd8
13 changed files with 24 additions and 21 deletions
@@ -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,
@@ -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:
+5
View File
@@ -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
@@ -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:
+1 -1
View File
@@ -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__)
@@ -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'"
)
@@ -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
@@ -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:
@@ -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
+1 -1
View File
@@ -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
+3 -3
View File
@@ -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 = []
+1 -2
View File
@@ -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__)
@@ -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