Closed michaeloliverx closed 4 years ago
The last thrown exception is raised once the retry window is over or the maximum number of retries is reached.
You could create a decorator that catches the exceptions you would like your callback to handle. The new decorator, for simplicity named add_callback
, should be the outside the retry decorator if you want to retry the exception and only handle unresolved exceptions in your callback. The add_callback
decorator should be inside the retry decorator if you like to handle each of the exceptions before retrying.
def add_callback(
*,
callback_on_exceptions: Union[Type[Exception], Tuple[Type[Exception], ...]],
callback: Callable[Union[Type[Exception], Tuple[Type[Exception], ...]], Any],
) -> Callable[[F], F]:
def decorator(f: F) -> F:
@functools.wraps(f)
def wrapper(*args: Any, **kwargs: Any) -> Any:
try:
return f(*args, **kwargs)
except callback_on_exceptions as e:
callback(e)
raise e
return cast(F, wrapper)
return decorator
Thanks for using our library! Hopefully, this helps to handle the problems you encounter.
It would be nice to have support for a callback (sync or async) which is called once the retry limit was reached.
Btw thanks for the library