diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2015-06-23 18:15:41 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2015-06-23 18:19:07 -0700 |
commit | 919d6e0011f2c725e0bf613d8b5fcf7e48b41cce (patch) | |
tree | a55c9bb40073c4e58e9979ee8792dc669473e6ea /retrying.py | |
parent | da33537e0850f997aea75c4dec010e2b5594071f (diff) | |
download | retrying-919d6e0011f2c725e0bf613d8b5fcf7e48b41cce.tar.gz |
Allow providing exception tuple for 'retry_on_exception' argument
It seems like a common pattern to have a function that is used
that just checks if the exception that is thrown is of a given type
so to avoid duplicating this pattern by all retry library users
just natively provide support for it.
Diffstat (limited to 'retrying.py')
-rw-r--r-- | retrying.py | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/retrying.py b/retrying.py index 3ed312d..32b8db6 100644 --- a/retrying.py +++ b/retrying.py @@ -23,6 +23,12 @@ import traceback MAX_WAIT = 1073741823 +def _retry_if_exception_of_type(retryable_types): + def _retry_if_exception_these_types(exception): + return isinstance(exception, retryable_types) + return _retry_if_exception_these_types + + def retry(*dargs, **dkw): """ Decorator function that instantiates the Retrying object @@ -127,9 +133,14 @@ class Retrying(object): if retry_on_exception is None: self._retry_on_exception = self.always_reject else: + # this allows for providing a tuple of exception types that + # should be allowed to retry on, and avoids having to create + # a callback that does the same thing + if isinstance(retry_on_exception, (tuple)): + retry_on_exception = _retry_if_exception_of_type( + retry_on_exception) self._retry_on_exception = retry_on_exception - # TODO simplify retrying by Exception types # retry on result filter if retry_on_result is None: self._retry_on_result = self.never_reject |