Skip to content

Commit 9b0faf6

Browse files
authored
Merge pull request #157 from Ensembl/thoas_db_selection
changed behaviour to raise error instead of using default db when re…
2 parents 5b42a6e + 6c9d007 commit 9b0faf6

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

common/db.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@
1717
import mongomock
1818
import grpc
1919

20+
from graphql_service.resolver.exceptions import (
21+
GenomeNotFoundError,
22+
FailedToConnectToGrpc,
23+
)
24+
25+
2026
from yagrc import reflector as yagrc_reflector
2127

28+
2229
from common.utils import process_release_version
2330

2431
logger = logging.getLogger(__name__)
@@ -37,9 +44,10 @@ def __init__(self, config):
3744
self.config = config
3845
self.mongo_client = MongoDbClient.connect_mongo(self.config)
3946

40-
def get_database_conn(self, grpc_model, uuid, force_grpc=False):
47+
def get_database_conn(self, grpc_model, uuid):
4148
grpc_response = None
42-
chosen_db = self.config.get("mongo_default_db")
49+
50+
chosen_db = None
4351
# Try to connect to gRPC
4452
try:
4553
grpc_response = grpc_model.get_release_by_genome_uuid(uuid)
@@ -48,23 +56,21 @@ def get_database_conn(self, grpc_model, uuid, force_grpc=False):
4856
logger.debug(
4957
"[get_database_conn] Couldn't connect to gRPC Host: %s", grpc_exp
5058
)
59+
raise FailedToConnectToGrpc(
60+
"Internal server error: Couldn't connect to gRPC Host"
61+
)
5162

52-
if force_grpc:
63+
if grpc_response and grpc_response.release_version:
5364
chosen_db = process_release_version(grpc_response)
5465
else:
55-
if grpc_response and grpc_response.release_version:
56-
chosen_db = process_release_version(grpc_response)
57-
else:
58-
# chosen_db value will fall back to the default value, which is 'mongo_default_db' that is in the config
59-
# if force_grpc is not True
60-
logger.warning(
61-
"[get_database_conn] Falling back to the default Mongo DB: '%s'",
62-
chosen_db,
63-
)
64-
65-
logger.debug("[get_database_conn] Connected to '%s' MongoDB", chosen_db)
66-
data_database_connection = self.mongo_client[chosen_db]
67-
return data_database_connection
66+
logger.warning("[get_database_conn] Release not found")
67+
raise GenomeNotFoundError({"genome_id": uuid})
68+
69+
if chosen_db is not None:
70+
logger.debug("[get_database_conn] Connected to '%s' MongoDB", chosen_db)
71+
data_database_connection = self.mongo_client[chosen_db]
72+
return data_database_connection
73+
raise GenomeNotFoundError({"genome_id": uuid})
6874

6975
@staticmethod
7076
def connect_mongo(config):

graphql_service/resolver/exceptions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def __init__(self, message: str):
235235
super().__init__(message)
236236

237237

238-
class FailedToConnectToGrpc(grpc.RpcError):
238+
class FailedToConnectToGrpc(GraphQLError):
239239
"""
240240
Exception raised when there is gRPC connection issue.
241241
"""
@@ -246,4 +246,5 @@ def __init__(self, message: str):
246246
Args:
247247
message: The error message describing the issue.
248248
"""
249-
super().__init__(message)
249+
self.extensions = {"code": "SERVER_CONNECTION_FAILED"}
250+
super().__init__(message, extensions=self.extensions)

0 commit comments

Comments
 (0)