-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add better error messages: If backend dependencies are missing, captu…
…re original ImportError and delay raising until class is initialized
- Loading branch information
Showing
1 changed file
with
28 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,46 @@ | ||
from logging import getLogger | ||
|
||
from aiohttp_client_cache.backends.base import ( # noqa: F401 | ||
BaseCache, | ||
CacheBackend, | ||
DictCache, | ||
ResponseOrKey, | ||
) | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
def get_placeholder_backend(original_exception): | ||
"""This creates a placeholder type for a backend class that does not have dependencies | ||
installed. This allows delaying the ImportError until init is called, rather then when imported. | ||
""" | ||
|
||
class PlaceholderBackend: | ||
def __init__(*args, **kwargs): | ||
logger.error('Dependencies are not installed for this backend') | ||
raise original_exception | ||
|
||
return PlaceholderBackend | ||
|
||
|
||
# Import all backends for which dependencies are installed | ||
try: | ||
from aiohttp_client_cache.backends.dynamodb import DynamoDBBackend | ||
except ImportError: | ||
DynamoDBBackend = None # type: ignore | ||
except ImportError as e: | ||
DynamoDBBackend = get_placeholder_backend(e) # type: ignore | ||
try: | ||
from aiohttp_client_cache.backends.gridfs import GridFSBackend | ||
except ImportError: | ||
GridFSBackend = None # type: ignore | ||
except ImportError as e: | ||
GridFSBackend = get_placeholder_backend(e) # type: ignore | ||
try: | ||
from aiohttp_client_cache.backends.mongo import MongoDBBackend | ||
except ImportError: | ||
MongoDBBackend = None # type: ignore | ||
except ImportError as e: | ||
MongoDBBackend = get_placeholder_backend(e) # type: ignore | ||
try: | ||
from aiohttp_client_cache.backends.redis import RedisBackend | ||
except ImportError: | ||
RedisBackend = None # type: ignore | ||
except ImportError as e: | ||
RedisBackend = get_placeholder_backend(e) # type: ignore | ||
try: | ||
from aiohttp_client_cache.backends.sqlite import SQLiteBackend | ||
except ImportError: | ||
SQLiteBackend = None # type: ignore | ||
except ImportError as e: | ||
SQLiteBackend = get_placeholder_backend(e) # type: ignore |