improve error handling

This commit is contained in:
David Rodenkirchen
2026-05-28 13:10:01 +02:00
parent edeefe072d
commit 11724ad0d9
+11
View File
@@ -1,8 +1,10 @@
import logging import logging
import sys
from beanie import init_beanie from beanie import init_beanie
from pymongo import AsyncMongoClient from pymongo import AsyncMongoClient
from pymongo.asynchronous.collection import AsyncCollection from pymongo.asynchronous.collection import AsyncCollection
from pymongo.errors import ServerSelectionTimeoutError, OperationFailure
from elm.types import User, Transaction, Ticket, Seat, CateringTypes from elm.types import User, Transaction, Ticket, Seat, CateringTypes
from elm.types.ConfigurationTypes import DatabaseConfiguration from elm.types.ConfigurationTypes import DatabaseConfiguration
@@ -30,6 +32,15 @@ class DatabaseService:
if self._client is None: if self._client is None:
self._client = AsyncMongoClient(mongo_uri) self._client = AsyncMongoClient(mongo_uri)
try:
await self._client.admin.command("ping")
except ServerSelectionTimeoutError:
print("Could not connect to mongodb")
sys.exit(1)
except OperationFailure:
print("Authentication with mongodb failed")
sys.exit(1)
self._database = self._client[ self._database = self._client[
self._db_config.database_name self._db_config.database_name
] ]