python-social-auth AuthCanceled exception

python-social-auth is a newer, derived version of django-social-auth.

AlexYar's answer can be slightly modified to work with python-social-auth by modify settings.py with following changes:

  1. Add a middleware to handle the SocialAuthException

    MIDDLEWARE_CLASSES += (
        'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',
    )
    
  2. URL to redirect to, when an exception occurred

    SOCIAL_AUTH_LOGIN_ERROR_URL = '/'
    
  3. Note that you also need to set

    DEBUG = False
    

That's all or read http://python-social-auth.readthedocs.org/en/latest/configuration/django.html#exceptions-middleware


you can create a middleware and catch any exceptions, exception list: https://github.com/omab/python-social-auth/blob/master/social/exceptions.py in this case your AuthCanceled Exception.

middleware.py



from social.apps.django_app.middleware import SocialAuthExceptionMiddleware
from django.shortcuts import HttpResponse
from social import exceptions as social_exceptions

class SocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
    def process_exception(self, request, exception):
        if hasattr(social_exceptions, 'AuthCanceled'):
            return HttpResponse("I'm the Pony %s" % exception)
        else:
            raise exception


settings.py



MIDDLEWARE_CLASSES = (
        .....
        'pat_to_middleware.SocialAuthExceptionMiddleware',
)