Rename MongoEngineConnectionError to ConnectionFailure (#2111)

I originally changed the exception name from `ConnectionError` to
`MongoEngineConnectionError` in
b02904ee75,
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 (
8855a510a8/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.
4983a9bde3/requests/exceptions.py (L32)
or
0be4d29206/redis/exceptions.py (L8)
This commit is contained in:
Stefan Wójcik
2019-06-30 09:23:32 +02:00
committed by GitHub
parent 2769967e1e
commit 9170eea784
4 changed files with 22 additions and 22 deletions

View File

@@ -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)