Skip to content

Commit

Permalink
Add better error messages: If backend dependencies are missing, captu…
Browse files Browse the repository at this point in the history
…re original ImportError and delay raising until class is initialized
  • Loading branch information
JWCook committed Feb 25, 2021
1 parent b859975 commit 7424ccf
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions aiohttp_client_cache/backends/__init__.py
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

0 comments on commit 7424ccf

Please sign in to comment.