Skip to content

Commit

Permalink
Add option to fail silently, even when the Flask app is in debug mode.
Browse files Browse the repository at this point in the history
Right now when the app is in debug mode, backend exceptions get
re-raised; this change adds the possibility to override this by using
the setting CACHE_IGNORE_BACKEND_ERRORS. This defaults to the inverse of
app.debug, to maintain current functionality.
  • Loading branch information
nfvs committed Sep 14, 2016
1 parent 1c60076 commit a15230e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ The following configuration values exist for Flask-Cache:
``CACHE_REDIS_URL`` URL to connect to Redis server.
Example ``redis://user:password@localhost:6379/2``.
Used only for RedisCache.
``CACHE_IGNORE_BACKEND_ERRORS`` Silently ignore backend exceptions, logging them instead.
Defaults to ``False`` when the Flask app is in debug mode,
and to ``True`` otherwise.
=============================== ==================================================================


Expand Down
15 changes: 9 additions & 6 deletions flask_cache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ def init_app(self, app, config=None):
config.setdefault('CACHE_ARGS', [])
config.setdefault('CACHE_TYPE', 'null')
config.setdefault('CACHE_NO_NULL_WARNING', False)
config.setdefault('CACHE_IGNORE_BACKEND_ERRORS', not app.debug)

self._ignore_backends_errors = config['CACHE_IGNORE_BACKEND_ERRORS']

if config['CACHE_TYPE'] == 'null' and not config['CACHE_NO_NULL_WARNING']:
warnings.warn("Flask-Cache: CACHE_TYPE is set to null, "
Expand Down Expand Up @@ -288,7 +291,7 @@ def decorated_function(*args, **kwargs):
cache_key = decorated_function.make_cache_key(*args, **kwargs)
rv = self.cache.get(cache_key)
except Exception:
if current_app.debug:
if not self._ignore_backends_errors:
raise
logger.exception("Exception possibly due to cache backend.")
return f(*args, **kwargs)
Expand All @@ -299,7 +302,7 @@ def decorated_function(*args, **kwargs):
self.cache.set(cache_key, rv,
timeout=decorated_function.cache_timeout)
except Exception:
if current_app.debug:
if not self._ignore_backends_errors:
raise
logger.exception("Exception possibly due to cache backend.")
return f(*args, **kwargs)
Expand Down Expand Up @@ -528,7 +531,7 @@ def decorated_function(*args, **kwargs):
cache_key = decorated_function.make_cache_key(f, *args, **kwargs)
rv = self.cache.get(cache_key)
except Exception:
if current_app.debug:
if not self._ignore_backends_errors:
raise
logger.exception("Exception possibly due to cache backend.")
return f(*args, **kwargs)
Expand All @@ -539,7 +542,7 @@ def decorated_function(*args, **kwargs):
self.cache.set(cache_key, rv,
timeout=decorated_function.cache_timeout)
except Exception:
if current_app.debug:
if not self._ignore_backends_errors:
raise
logger.exception("Exception possibly due to cache backend.")
return rv
Expand Down Expand Up @@ -666,7 +669,7 @@ def add(self, b):
cache_key = f.make_cache_key(f.uncached, *args, **kwargs)
self.cache.delete(cache_key)
except Exception:
if current_app.debug:
if not self._ignore_backends_errors:
raise
logger.exception("Exception possibly due to cache backend.")

Expand All @@ -689,6 +692,6 @@ def delete_memoized_verhash(self, f, *args):
try:
self._memoize_version(f, delete=True)
except Exception:
if current_app.debug:
if not self._ignore_backends_errors:
raise
logger.exception("Exception possibly due to cache backend.")

0 comments on commit a15230e

Please sign in to comment.