Skip to content

Commit

Permalink
Merge branch 'add-exception-to-details'
Browse files Browse the repository at this point in the history
  • Loading branch information
bgreen-litl committed Oct 5, 2022
2 parents d9b9f01 + e7677e8 commit 31d973f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.rst
Expand Up @@ -288,7 +288,9 @@ In the case of the ``on_exception`` decorator, all ``on_backoff`` and
``on_giveup`` handlers are called from within the except block for the
exception being handled. Therefore exception info is available to the
handler functions via the python standard library, specifically
``sys.exc_info()`` or the ``traceback`` module.
``sys.exc_info()`` or the ``traceback`` module. The exception is also
available at the *exception* key in the `details` dict passed to the
handlers.

Asynchronous code
-----------------
Expand Down
7 changes: 4 additions & 3 deletions backoff/_async.py
Expand Up @@ -156,7 +156,7 @@ async def retry(*args, **kwargs):
elapsed >= max_time_value)

if giveup_result or max_tries_exceeded or max_time_exceeded:
await _call_handlers(on_giveup, **details)
await _call_handlers(on_giveup, **details, exception=e)
if raise_on_giveup:
raise
return None
Expand All @@ -165,10 +165,11 @@ async def retry(*args, **kwargs):
seconds = _next_wait(wait, e, jitter, elapsed,
max_time_value)
except StopIteration:
await _call_handlers(on_giveup, **details)
await _call_handlers(on_giveup, **details, exception=e)
raise e

await _call_handlers(on_backoff, **details, wait=seconds)
await _call_handlers(on_backoff, **details, wait=seconds,
exception=e)

# Note: there is no convenient way to pass explicit event
# loop to decorator, so here we assume that either default
Expand Down
7 changes: 4 additions & 3 deletions backoff/_sync.py
Expand Up @@ -109,7 +109,7 @@ def retry(*args, **kwargs):
elapsed >= max_time_value)

if giveup(e) or max_tries_exceeded or max_time_exceeded:
_call_handlers(on_giveup, **details)
_call_handlers(on_giveup, **details, exception=e)
if raise_on_giveup:
raise
return None
Expand All @@ -118,10 +118,11 @@ def retry(*args, **kwargs):
seconds = _next_wait(wait, e, jitter, elapsed,
max_time_value)
except StopIteration:
_call_handlers(on_giveup, **details)
_call_handlers(on_giveup, **details, exception=e)
raise e

_call_handlers(on_backoff, **details, wait=seconds)
_call_handlers(on_backoff, **details, wait=seconds,
exception=e)

time.sleep(seconds)
else:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_backoff.py
Expand Up @@ -299,7 +299,9 @@ def succeeder(*args, **kwargs):
for i in range(2):
details = backoffs[i]
elapsed = details.pop('elapsed')
exception = details.pop('exception')
assert isinstance(elapsed, float)
assert isinstance(exception, ValueError)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': succeeder._target,
Expand Down Expand Up @@ -345,7 +347,9 @@ def exceptor(*args, **kwargs):

details = giveups[0]
elapsed = details.pop('elapsed')
exception = details.pop('exception')
assert isinstance(elapsed, float)
assert isinstance(exception, ValueError)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': exceptor._target,
Expand Down Expand Up @@ -517,7 +521,9 @@ def succeeder(*args, **kwargs):
for i in range(2):
details = backoffs[i]
elapsed = details.pop('elapsed')
exception = details.pop('exception')
assert isinstance(elapsed, float)
assert isinstance(exception, ValueError)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': succeeder._target,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_backoff_async.py
Expand Up @@ -255,7 +255,9 @@ async def succeeder(*args, **kwargs):
for i in range(2):
details = log['backoff'][i]
elapsed = details.pop('elapsed')
exception = details.pop('exception')
assert isinstance(elapsed, float)
assert isinstance(exception, ValueError)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': succeeder._target,
Expand Down Expand Up @@ -302,7 +304,9 @@ async def exceptor(*args, **kwargs):

details = log['giveup'][0]
elapsed = details.pop('elapsed')
exception = details.pop('exception')
assert isinstance(elapsed, float)
assert isinstance(exception, ValueError)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': exceptor._target,
Expand Down Expand Up @@ -521,7 +525,9 @@ async def succeeder(*args, **kwargs):
for i in range(2):
details = log['backoff'][i]
elapsed = details.pop('elapsed')
exception = details.pop('exception')
assert isinstance(elapsed, float)
assert isinstance(exception, ValueError)
assert details == {'args': (1, 2, 3),
'kwargs': {'foo': 1, 'bar': 2},
'target': succeeder._target,
Expand Down

0 comments on commit 31d973f

Please sign in to comment.