From a15230ef98ac09e0b19841c069e22e48a805c378 Mon Sep 17 00:00:00 2001 From: Nuno Santos Date: Wed, 14 Sep 2016 18:25:13 +0200 Subject: [PATCH] Add option to fail silently, even when the Flask app is in debug mode. 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. --- docs/index.rst | 3 +++ flask_cache/__init__.py | 15 +++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 2ed3a23..0cc4181 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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. =============================== ================================================================== diff --git a/flask_cache/__init__.py b/flask_cache/__init__.py index 85d90e3..c5e01a5 100644 --- a/flask_cache/__init__.py +++ b/flask_cache/__init__.py @@ -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, " @@ -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) @@ -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) @@ -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) @@ -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 @@ -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.") @@ -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.")