Decorator to handle exceptions.

def oauth_error_handler(f):
    """Decorator to handle exceptions."""
    @wraps(f)
    def inner(*args, **kwargs):
        # OAuthErrors should not happen, so they are not caught here. Hence
        # they will result in a 500 Internal Server Error which is what we
        # are interested in.
        try:
            return f(*args, **kwargs)
        except OAuthClientError as e:
            current_app.logger.warning(e.message, exc_info=True)
            return oauth2_handle_error(
                e.remote, e.response, e.code, e.uri, e.description
            )
        except OAuthCERNRejectedAccountError as e:
            current_app.logger.warning(e.message, exc_info=True)
            flash(_('CERN account not allowed.'),
                  category='danger')
            return redirect('/')
        except OAuthRejectedRequestError:
            flash(_('You rejected the authentication request.'),
                  category='info')
            return redirect('/')
        except AlreadyLinkedError:
            flash(_('External service is already linked to another account.'),
                  category='danger')
            return redirect(url_for('invenio_oauthclient_settings.index'))
    return inner