diff --git a/common/db.py b/common/db.py index 3987651..684febb 100644 --- a/common/db.py +++ b/common/db.py @@ -17,6 +17,10 @@ import mongomock import grpc from ensembl.production.metadata.grpc import ensembl_metadata_pb2_grpc +from graphql_service.resolver.exceptions import ( + GenomeNotFoundError, + FailedToConnectToGrpc, +) from common.utils import process_release_version @@ -36,9 +40,10 @@ def __init__(self, config): self.config = config self.mongo_client = MongoDbClient.connect_mongo(self.config) - def get_database_conn(self, grpc_model, uuid, force_grpc=False): + def get_database_conn(self, grpc_model, uuid): grpc_response = None - chosen_db = self.config.get("mongo_default_db") + + chosen_db = None # Try to connect to gRPC try: grpc_response = grpc_model.get_release_by_genome_uuid(uuid) @@ -47,23 +52,21 @@ def get_database_conn(self, grpc_model, uuid, force_grpc=False): logger.debug( "[get_database_conn] Couldn't connect to gRPC Host: %s", grpc_exp ) + raise FailedToConnectToGrpc( + "Internal server error: Couldn't connect to gRPC Host" + ) - if force_grpc: + if grpc_response and grpc_response.release_version: chosen_db = process_release_version(grpc_response) else: - if grpc_response and grpc_response.release_version: - chosen_db = process_release_version(grpc_response) - else: - # chosen_db value will fall back to the default value, which is 'mongo_default_db' that is in the config - # if force_grpc is not True - logger.warning( - "[get_database_conn] Falling back to the default Mongo DB: '%s'", - chosen_db, - ) - - logger.debug("[get_database_conn] Connected to '%s' MongoDB", chosen_db) - data_database_connection = self.mongo_client[chosen_db] - return data_database_connection + logger.warning("[get_database_conn] Release not found") + raise GenomeNotFoundError({"genome_id": uuid}) + + if chosen_db is not None: + logger.debug("[get_database_conn] Connected to '%s' MongoDB", chosen_db) + data_database_connection = self.mongo_client[chosen_db] + return data_database_connection + raise GenomeNotFoundError({"genome_id": uuid}) @staticmethod def connect_mongo(config): diff --git a/graphql_service/resolver/exceptions.py b/graphql_service/resolver/exceptions.py index 8df3053..66e002f 100644 --- a/graphql_service/resolver/exceptions.py +++ b/graphql_service/resolver/exceptions.py @@ -235,7 +235,7 @@ def __init__(self, message: str): super().__init__(message) -class FailedToConnectToGrpc(grpc.RpcError): +class FailedToConnectToGrpc(GraphQLError): """ Exception raised when there is gRPC connection issue. """ @@ -246,4 +246,5 @@ def __init__(self, message: str): Args: message: The error message describing the issue. """ - super().__init__(message) + self.extensions = {"code": "SERVER_CONNECTION_FAILED"} + super().__init__(message, extensions=self.extensions)