Skip to content

Commit 3d149ad

Browse files
committed
give a nicer error if URI has no dbname
1 parent 6acc14b commit 3d149ad

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

flask_pymongo/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ def init_app(self, app, uri=None, *args, **kwargs):
149149
parsed_uri = uri_parser.parse_uri(uri)
150150
database_name = parsed_uri["database"]
151151

152+
# Avoid a more confusing error later when we try to get the DB
153+
if not database_name:
154+
raise ValueError("Your URI must specify a database name")
155+
152156
# Try to delay connecting, in case the app is loaded before forking, per
153157
# http://api.mongodb.com/python/current/faq.html#is-pymongo-fork-safe
154158
kwargs.setdefault("connect", False)

flask_pymongo/tests/test_config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ def test_it_doesnt_connect_by_default(self):
8181
with pytest.raises(CouldNotConnect):
8282
_wait_until_connected(mongo, timeout=0.2)
8383

84+
def test_it_requires_db_name_in_uri(self):
85+
uri = "mongodb://localhost:{}".format(self.port)
86+
87+
with pytest.raises(ValueError):
88+
flask_pymongo.PyMongo(self.app, uri)
89+
8490

8591
def _wait_until_connected(mongo, timeout=1.0):
8692
start = time.time()

0 commit comments

Comments
 (0)