Skip to content
This repository was archived by the owner on Aug 8, 2025. It is now read-only.

Commit 31d973f

Browse files
committed
Merge branch 'add-exception-to-details'
2 parents d9b9f01 + e7677e8 commit 31d973f

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ In the case of the ``on_exception`` decorator, all ``on_backoff`` and
288288
``on_giveup`` handlers are called from within the except block for the
289289
exception being handled. Therefore exception info is available to the
290290
handler functions via the python standard library, specifically
291-
``sys.exc_info()`` or the ``traceback`` module.
291+
``sys.exc_info()`` or the ``traceback`` module. The exception is also
292+
available at the *exception* key in the `details` dict passed to the
293+
handlers.
292294

293295
Asynchronous code
294296
-----------------

backoff/_async.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ async def retry(*args, **kwargs):
156156
elapsed >= max_time_value)
157157

158158
if giveup_result or max_tries_exceeded or max_time_exceeded:
159-
await _call_handlers(on_giveup, **details)
159+
await _call_handlers(on_giveup, **details, exception=e)
160160
if raise_on_giveup:
161161
raise
162162
return None
@@ -165,10 +165,11 @@ async def retry(*args, **kwargs):
165165
seconds = _next_wait(wait, e, jitter, elapsed,
166166
max_time_value)
167167
except StopIteration:
168-
await _call_handlers(on_giveup, **details)
168+
await _call_handlers(on_giveup, **details, exception=e)
169169
raise e
170170

171-
await _call_handlers(on_backoff, **details, wait=seconds)
171+
await _call_handlers(on_backoff, **details, wait=seconds,
172+
exception=e)
172173

173174
# Note: there is no convenient way to pass explicit event
174175
# loop to decorator, so here we assume that either default

backoff/_sync.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def retry(*args, **kwargs):
109109
elapsed >= max_time_value)
110110

111111
if giveup(e) or max_tries_exceeded or max_time_exceeded:
112-
_call_handlers(on_giveup, **details)
112+
_call_handlers(on_giveup, **details, exception=e)
113113
if raise_on_giveup:
114114
raise
115115
return None
@@ -118,10 +118,11 @@ def retry(*args, **kwargs):
118118
seconds = _next_wait(wait, e, jitter, elapsed,
119119
max_time_value)
120120
except StopIteration:
121-
_call_handlers(on_giveup, **details)
121+
_call_handlers(on_giveup, **details, exception=e)
122122
raise e
123123

124-
_call_handlers(on_backoff, **details, wait=seconds)
124+
_call_handlers(on_backoff, **details, wait=seconds,
125+
exception=e)
125126

126127
time.sleep(seconds)
127128
else:

tests/test_backoff.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ def succeeder(*args, **kwargs):
299299
for i in range(2):
300300
details = backoffs[i]
301301
elapsed = details.pop('elapsed')
302+
exception = details.pop('exception')
302303
assert isinstance(elapsed, float)
304+
assert isinstance(exception, ValueError)
303305
assert details == {'args': (1, 2, 3),
304306
'kwargs': {'foo': 1, 'bar': 2},
305307
'target': succeeder._target,
@@ -345,7 +347,9 @@ def exceptor(*args, **kwargs):
345347

346348
details = giveups[0]
347349
elapsed = details.pop('elapsed')
350+
exception = details.pop('exception')
348351
assert isinstance(elapsed, float)
352+
assert isinstance(exception, ValueError)
349353
assert details == {'args': (1, 2, 3),
350354
'kwargs': {'foo': 1, 'bar': 2},
351355
'target': exceptor._target,
@@ -517,7 +521,9 @@ def succeeder(*args, **kwargs):
517521
for i in range(2):
518522
details = backoffs[i]
519523
elapsed = details.pop('elapsed')
524+
exception = details.pop('exception')
520525
assert isinstance(elapsed, float)
526+
assert isinstance(exception, ValueError)
521527
assert details == {'args': (1, 2, 3),
522528
'kwargs': {'foo': 1, 'bar': 2},
523529
'target': succeeder._target,

tests/test_backoff_async.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ async def succeeder(*args, **kwargs):
255255
for i in range(2):
256256
details = log['backoff'][i]
257257
elapsed = details.pop('elapsed')
258+
exception = details.pop('exception')
258259
assert isinstance(elapsed, float)
260+
assert isinstance(exception, ValueError)
259261
assert details == {'args': (1, 2, 3),
260262
'kwargs': {'foo': 1, 'bar': 2},
261263
'target': succeeder._target,
@@ -302,7 +304,9 @@ async def exceptor(*args, **kwargs):
302304

303305
details = log['giveup'][0]
304306
elapsed = details.pop('elapsed')
307+
exception = details.pop('exception')
305308
assert isinstance(elapsed, float)
309+
assert isinstance(exception, ValueError)
306310
assert details == {'args': (1, 2, 3),
307311
'kwargs': {'foo': 1, 'bar': 2},
308312
'target': exceptor._target,
@@ -521,7 +525,9 @@ async def succeeder(*args, **kwargs):
521525
for i in range(2):
522526
details = log['backoff'][i]
523527
elapsed = details.pop('elapsed')
528+
exception = details.pop('exception')
524529
assert isinstance(elapsed, float)
530+
assert isinstance(exception, ValueError)
525531
assert details == {'args': (1, 2, 3),
526532
'kwargs': {'foo': 1, 'bar': 2},
527533
'target': succeeder._target,

0 commit comments

Comments
 (0)