def add_errback(future, callback, loop=None):
    '''Add a ``callback`` to a ``future`` executed only if an exception
    or cancellation has occurred.'''
    def _error_back(fut):
        if fut._exception:
            callback(fut.exception())
        elif fut.cancelled():
            callback(CancelledError())

    future = ensure_future(future, loop=None)
    future.add_done_callback(_error_back)
    return future
Add a ``callback`` to a ``future`` executed only if an exception
    or cancellation has occurred.