Fail fast when db name is invalid

Without this commit save operation on first document would fail instead of immediate failure upon connection attempt. Such later failure is much less obvious.
This commit is contained in:
Yurii Andrieiev
2019-04-07 02:02:26 +03:00
parent 827de76345
commit b5213097e8
4 changed files with 45 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
from pymongo import MongoClient, ReadPreference, uri_parser
from pymongo.database import _check_name
import six
from mongoengine.pymongo_support import IS_PYMONGO_3
@@ -28,6 +29,16 @@ _connections = {}
_dbs = {}
def check_db_name(name):
"""Check if a database name is valid.
This functionality is copied from pymongo Database class constructor.
"""
if not isinstance(name, six.string_types):
raise TypeError('name must be an instance of %s' % six.string_types)
elif name != '$external':
_check_name(name)
def register_connection(alias, db=None, name=None, host=None, port=None,
read_preference=READ_PREFERENCE,
username=None, password=None,
@@ -69,6 +80,7 @@ def register_connection(alias, db=None, name=None, host=None, port=None,
'authentication_mechanism': authentication_mechanism
}
check_db_name(conn_settings['name'])
conn_host = conn_settings['host']
# Host can be a list or a string, so if string, force to a list.