Skip to content

Commit

Permalink
Merge pull request #613 from tmarice/master
Browse files Browse the repository at this point in the history
Log traceback if exceptions are ignored
  • Loading branch information
WisdomPill authored Nov 7, 2022
2 parents 66650af + 52fc78d commit 040d85e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/611.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Print full exception traceback when logging ignored exceptions
2 changes: 1 addition & 1 deletion django_redis/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _decorator(self, *args, **kwargs):
except ConnectionInterrupted as e:
if self._ignore_exceptions:
if self._log_ignored_exceptions:
self.logger.error(str(e))
self.logger.exception("Exception ignored")

return return_value
raise e.__cause__
Expand Down
15 changes: 14 additions & 1 deletion tests/test_cache_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest
from django.core.cache import caches
from pytest import LogCaptureFixture
from pytest_django.fixtures import SettingsWrapper
from redis.exceptions import ConnectionError

Expand All @@ -22,8 +23,10 @@ def reverse_key(key: str) -> str:
def ignore_exceptions_cache(settings: SettingsWrapper) -> RedisCache:
caches_setting = copy.deepcopy(settings.CACHES)
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = True
caches_setting["doesnotexist"]["OPTIONS"]["LOG_IGNORED_EXCEPTIONS"] = True
settings.CACHES = caches_setting
settings.DJANGO_REDIS_IGNORE_EXCEPTIONS = True
settings.DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS = True
return cast(RedisCache, caches["doesnotexist"])


Expand All @@ -34,12 +37,22 @@ def test_get_django_omit_exceptions_many_returns_default_arg(
assert ignore_exceptions_cache.get_many(["key1", "key2", "key3"]) == {}


def test_get_django_omit_exceptions(ignore_exceptions_cache: RedisCache):
def test_get_django_omit_exceptions(
caplog: LogCaptureFixture, ignore_exceptions_cache: RedisCache
):
assert ignore_exceptions_cache._ignore_exceptions is True
assert ignore_exceptions_cache._log_ignored_exceptions is True

assert ignore_exceptions_cache.get("key") is None
assert ignore_exceptions_cache.get("key", "default") == "default"
assert ignore_exceptions_cache.get("key", default="default") == "default"

assert len(caplog.records) == 3
assert all(
record.levelname == "ERROR" and record.msg == "Exception ignored"
for record in caplog.records
)


def test_get_django_omit_exceptions_priority_1(settings: SettingsWrapper):
caches_setting = copy.deepcopy(settings.CACHES)
Expand Down

0 comments on commit 040d85e

Please sign in to comment.