如何处理 Python Social Auth 的异常?

11

我该如何处理Python Social Auth中的AuthAlreadyAssociated异常?

我找到的所有答案都是针对Django Social Auth,但自那时起已经发生了许多变化。

1个回答

16

您可以在应用程序的 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',
   )

如果问题在某个地方,但是它会在这里引发。 - Raja Simon
如果您想显式处理AuthAlreadyAssociated异常,请使用以下示例:if isinstance(exception, social_exceptions.AuthAlreadyAssociated):。例如重定向:return redirect(reverse('index')) - Tim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接