Skip to content

Commit

Permalink
Catch error for invalid URI. (#2201)
Browse files Browse the repository at this point in the history
* Catch error for invalid URI.

This is a second attempt to resolve issue#2197 and should enable connecting to a MongoDB Atlas Cluster via its connection string.
  • Loading branch information
JPBergsma authored Dec 27, 2024
1 parent 7798f67 commit 2035851
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions optimade/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,12 +497,16 @@ def align_mongo_uri_and_mongo_database(self) -> "ServerConfig":
"mongodb://"
) or self.mongo_uri.startswith("mongodb+srv://"):
self.mongo_uri = f"mongodb://{self.mongo_uri}"

uri: dict[str, Any] = pymongo.uri_parser.parse_uri(
self.mongo_uri, warn=True
)
if uri.get("database"):
self.mongo_database = uri["database"]
try:
uri: dict[str, Any] = pymongo.uri_parser.parse_uri(
self.mongo_uri, warn=True
)
if uri.get("database"):
self.mongo_database = uri["database"]
except pymongo.errors.InvalidURI as error_msg:
warnings.warn(
f"The uri {self.mongo_uri} may be invalid.\n{error_msg}"
)

return self

Expand Down

0 comments on commit 2035851

Please sign in to comment.