Skip to content

Commit

Permalink
Add ability to override default error handling behaviour
Browse files Browse the repository at this point in the history
By default errors raised in side-effects functions are logged and then thrown
away. This is by design. Side-effects should not, by definition, halt the normal
flow, however in certain circumstances (e.g. testing) this may not be
desirable.

This commit introduces a setting 'SUPPRESS_ERRORS', which is True
by default, but can be overridden by setting the env var 'SIDE_EFFECTS_SUPPRESS_ERRORS'
to 'False'.
  • Loading branch information
hugorodgerbrown committed Jul 28, 2017
1 parent 0c1a01f commit e544edd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
11 changes: 9 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,27 @@

setup(
name="django-side-effects",
version="0.2",
version="0.2.1",
packages=find_packages(),
include_package_data=True,
install_requires=['Django>=1.8'],
description='Django app for managing external side effects.',
long_description=README,
url='https://github.com/yunojuno/django-side-effects',
install_requires=[
'django>=1.9',
'python-env-utils'
],
author='YunoJuno',
author_email='[email protected]',
license='MIT',
maintainer='YunoJuno',
maintainer_email='[email protected]',
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 1.9',
'Framework :: Django :: 1.10',
'Framework :: Django :: 1.11',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
Expand Down
2 changes: 2 additions & 0 deletions side_effects/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# -*- coding: utf-8 -*-
default_app_config = 'side_effects.apps.SideEffectsConfig'
10 changes: 7 additions & 3 deletions side_effects/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
This module contains the Registry class that is responsible for managing
all of the registered side-effects.
"""
from __future__ import unicode_literals

from collections import defaultdict
import logging
import threading

from . import settings


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -91,7 +92,10 @@ def _run_func(func, *args, **kwargs):
try:
func(*args, **kwargs)
except Exception:
logger.exception("Error running side_effect function '%s'", fname(func))
if settings.SUPPRESS_ERRORS:
logger.exception("Error running side_effect function '%s'", fname(func))
else:
raise


# global registry
Expand Down
11 changes: 11 additions & 0 deletions side_effects/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from env_utils import get_bool

from django.conf import settings

# If True then do not raise exceptions caught when running
# side-effects, but just log them instead.
# Default = True
SUPPRESS_ERRORS = getattr(
settings, 'SIDE_EFFECTS_SUPPRESS_ERRORS',
get_bool('SIDE_EFFECTS_SUPPRESS_ERRORS', True)
)
11 changes: 8 additions & 3 deletions side_effects/tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import absolute_import

import mock

from django.test import TestCase

from . import registry, decorators
from .compat import mock
from . import registry, decorators, settings


def test_func_no_docstring(arg1, kwarg1=None):
Expand Down Expand Up @@ -101,6 +102,10 @@ def test__run_func(self):
registry._run_func(test_func_exception)
self.assertEqual(mock_logger.exception.call_count, 1)

# if the func raises an exception we should log it but not fail
settings.SUPPRESS_ERRORS = False
self.assertRaises(Exception, registry._run_func, test_func_exception)


class RegistryTests(TestCase):

Expand Down
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py{27,36}-django{18,19,110,111}
envlist = py{27,36}-django{19,110,111}

[testenv]
deps =
Expand All @@ -11,7 +11,6 @@ deps =
django111: Django==1.11

commands=
python --version
coverage erase
coverage run --include=side_effects/* manage.py test side_effects
coverage report

0 comments on commit e544edd

Please sign in to comment.