def on_exception(wait_gen,
                 exception,
                 max_tries=None,
                 jitter=full_jitter,
                 giveup=lambda e: False,
                 on_success=None,
                 on_backoff=None,
                 on_giveup=None,
                 **wait_gen_kwargs):
    """Returns decorator for backoff and retry triggered by exception.

    Args:
        wait_gen: A generator yielding successive wait times in
            seconds.
        exception: An exception type (or tuple of types) which triggers
            backoff.
        max_tries: The maximum number of attempts to make before giving
            up. Once exhausted, the exception will be allowed to escape.
            The default value of None means their is no limit to the
            number of tries. If a callable is passed, it will be
            evaluated at runtime and its return value used.
        jitter: A function of the value yielded by wait_gen returning
            the actual time to wait. This distributes wait times
            stochastically in order to avoid timing collisions across
            concurrent clients. Wait times are jittered by default
            using the full_jitter function. Jittering may be disabled
            altogether by passing jitter=None.
        giveup: Function accepting an exception instance and
            returning whether or not to give up. Optional. The default
            is to always continue.
        on_success: Callable (or iterable of callables) with a unary
            signature to be called in the event of success. The
            parameter is a dict containing details about the invocation.
        on_backoff: Callable (or iterable of callables) with a unary
            signature to be called in the event of a backoff. The
            parameter is a dict containing details about the invocation.
        on_giveup: Callable (or iterable of callables) with a unary
            signature to be called in the event that max_tries
            is exceeded.  The parameter is a dict containing details
            about the invocation.
        **wait_gen_kwargs: Any additional keyword args specified will be
            passed to wait_gen when it is initialized.  Any callable
            args will first be evaluated and their return values passed.
            This is useful for runtime configuration.
    """
    success_hdlrs = _handlers(on_success)
    backoff_hdlrs = _handlers(on_backoff, _log_backoff)
    giveup_hdlrs = _handlers(on_giveup, _log_giveup)

    def decorate(target):

        @functools.wraps(target)
        def retry(*args, **kwargs):
            # change names because python 2.x doesn't have nonlocal
            max_tries_ = _maybe_call(max_tries)

            # there are no dictionary comprehensions in python 2.6
            wait = wait_gen(**dict((k, _maybe_call(v))
                                   for k, v in wait_gen_kwargs.items()))

            tries = 0
            while True:
                try:
                    tries += 1
                    ret = target(*args, **kwargs)
                except exception as e:
                    if giveup(e) or tries == max_tries_:
                        for hdlr in giveup_hdlrs:
                            hdlr({'target': target,
                                  'args': args,
                                  'kwargs': kwargs,
                                  'tries': tries})
                        raise

                    value = next(wait)
                    try:
                        if jitter is not None:
                            seconds = jitter(value)
                        else:
                            seconds = value
                    except TypeError:
                        # support deprecated nullary jitter function signature
                        # which returns a delta rather than a jittered value
                        seconds = value + jitter()

                    for hdlr in backoff_hdlrs:
                        hdlr({'target': target,
                              'args': args,
                              'kwargs': kwargs,
                              'tries': tries,
                              'wait': seconds})

                    time.sleep(seconds)
                else:
                    for hdlr in success_hdlrs:
                        hdlr({'target': target,
                              'args': args,
                              'kwargs': kwargs,
                              'tries': tries})

                    return ret

        return retry

    # Return a function which decorates a target with a retry loop.
    return decorate
Returns decorator for backoff and retry triggered by exception.

    Args:
        wait_gen: A generator yielding successive wait times in
            seconds.
        exception: An exception type (or tuple of types) which triggers
            backoff.
        max_tries: The maximum number of attempts to make before giving
            up. Once exhausted, the exception will be allowed to escape.
            The default value of None means their is no limit to the
            number of tries. If a callable is passed, it will be
            evaluated at runtime and its return value used.
        jitter: A function of the value yielded by wait_gen returning
            the actual time to wait. This distributes wait times
            stochastically in order to avoid timing collisions across
            concurrent clients. Wait times are jittered by default
            using the full_jitter function. Jittering may be disabled
            altogether by passing jitter=None.
        giveup: Function accepting an exception instance and
            returning whether or not to give up. Optional. The default
            is to always continue.
        on_success: Callable (or iterable of callables) with a unary
            signature to be called in the event of success. The
            parameter is a dict containing details about the invocation.
        on_backoff: Callable (or iterable of callables) with a unary
            signature to be called in the event of a backoff. The
            parameter is a dict containing details about the invocation.
        on_giveup: Callable (or iterable of callables) with a unary
            signature to be called in the event that max_tries
            is exceeded.  The parameter is a dict containing details
            about the invocation.
        **wait_gen_kwargs: Any additional keyword args specified will be
            passed to wait_gen when it is initialized.  Any callable
            args will first be evaluated and their return values passed.
            This is useful for runtime configuration.