Rename MongoEngineConnectionError to ConnectionFailure (#2111)
I originally changed the exception name from `ConnectionError` to `MongoEngineConnectionError` in https://github.com/MongoEngine/mongoengine/pull/1428/commits/b02904ee750a30f8e2246a326376b40358543101, inspired by landscape.io's package health report, which argued that `ConnectionError` is already a built-in exception in Python 3 (which it is: https://docs.python.org/3/library/exceptions.html#ConnectionError). I do agree that we shouldn't override built-in exceptions. [0] That said, it’s silly to add a "MongoEngine" prefix to any class within the `mongoengine` module (and *especially* to *just one* exception class out of many). I've decided to do what PyMongo does ( https://github.com/mongodb/mongo-python-driver/blob/8855a510a80a30268ffd4b90be65fb26929648e2/pymongo/errors.py#L59) and call this exception `ConnectionFailure`. Note that this is a breaking change and people will need to rename `MongoEngineConnectionError`s in their code to `ConnectionFailure`. Moreover, if they use PyMongo's `ConnectionFailure` for anything, they'll need to take extra care to avoid conflicts, e.g. by using: ``` from mongoengine import ConnectionFailure as MongoEngineConnectionFailure ``` [0] Note that some popular packages still overwrite `ConnectionError`, e.g. https://github.com/kennethreitz/requests/blob/4983a9bde39c6320aa4f3e34e50dac6e263dab6f/requests/exceptions.py#L32 or https://github.com/andymccurdy/redis-py/blob/0be4d2920684345eb52115c7142c39d65356e7d4/redis/exceptions.py#L8
This commit is contained in:
@@ -5,7 +5,7 @@ import six
|
||||
__all__ = [
|
||||
"DEFAULT_CONNECTION_NAME",
|
||||
"DEFAULT_DATABASE_NAME",
|
||||
"MongoEngineConnectionError",
|
||||
"ConnectionFailure",
|
||||
"connect",
|
||||
"disconnect",
|
||||
"disconnect_all",
|
||||
@@ -27,7 +27,7 @@ _dbs = {}
|
||||
READ_PREFERENCE = ReadPreference.PRIMARY
|
||||
|
||||
|
||||
class MongoEngineConnectionError(Exception):
|
||||
class ConnectionFailure(Exception):
|
||||
"""Error raised when the database connection can't be established or
|
||||
when a connection with a requested alias can't be retrieved.
|
||||
"""
|
||||
@@ -252,13 +252,13 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
|
||||
return _connections[alias]
|
||||
|
||||
# Validate that the requested alias exists in the _connection_settings.
|
||||
# Raise MongoEngineConnectionError if it doesn't.
|
||||
# Raise ConnectionFailure if it doesn't.
|
||||
if alias not in _connection_settings:
|
||||
if alias == DEFAULT_CONNECTION_NAME:
|
||||
msg = "You have not defined a default connection"
|
||||
else:
|
||||
msg = 'Connection with alias "%s" has not been defined' % alias
|
||||
raise MongoEngineConnectionError(msg)
|
||||
raise ConnectionFailure(msg)
|
||||
|
||||
def _clean_settings(settings_dict):
|
||||
irrelevant_fields_set = {
|
||||
@@ -305,14 +305,12 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
|
||||
def _create_connection(alias, connection_class, **connection_settings):
|
||||
"""
|
||||
Create the new connection for this alias. Raise
|
||||
MongoEngineConnectionError if it can't be established.
|
||||
ConnectionFailure if it can't be established.
|
||||
"""
|
||||
try:
|
||||
return connection_class(**connection_settings)
|
||||
except Exception as e:
|
||||
raise MongoEngineConnectionError(
|
||||
"Cannot connect to database %s :\n%s" % (alias, e)
|
||||
)
|
||||
raise ConnectionFailure("Cannot connect to database %s :\n%s" % (alias, e))
|
||||
|
||||
|
||||
def _find_existing_connection(connection_settings):
|
||||
@@ -393,7 +391,7 @@ def connect(db=None, alias=DEFAULT_CONNECTION_NAME, **kwargs):
|
||||
u"A different connection with alias `{}` was already "
|
||||
u"registered. Use disconnect() first"
|
||||
).format(alias)
|
||||
raise MongoEngineConnectionError(err_msg)
|
||||
raise ConnectionFailure(err_msg)
|
||||
else:
|
||||
register_connection(alias, db, **kwargs)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user