如何处理Python Social Auth上的异常

时间:2013-10-10 20:51:31

标签: python django authentication social

如何在AuthAlreadyAssociated上处理此类异常Python Social Auth

我找到的所有答案都是针对Django Social Auth的,但自从写完之后,似乎已经发生了很多变化。

1 个答案:

答案 0 :(得分:14)

您可以在应用的middleware.py中创建新的中间件:

from social_django.middleware import SocialAuthExceptionMiddleware
from social_core import exceptions as social_exceptions     
from django.http import HttpResponse

class MySocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
    def process_exception(self, request, exception):
        if hasattr(social_exceptions, exception.__class__.__name__):
            # Here you can handle the exception as you wish
            return HttpResponse("Exception %s while processing your social account." % exception)
        else:
            return super(MySocialAuthExceptionMiddleware, self).process_exception(request, exception)

并将其路径添加到settings.py

MIDDLEWARE_CLASSES = (
    ...
    'path.to.MySocialAuthExceptionMiddleware',
   )