我想从 django-rest-auth 中捕获一个异常。类 rest_auth.serializers.LoginSerializer 抛出各种异常,都是 exceptions.ValidationError
msg = _('Must include "email" and "password".')
raise exceptions.ValidationError(msg)
msg = _('Must include "username" and "password".')
raise exceptions.ValidationError(msg)
raise serializers.ValidationError(_('E-mail is not verified.'))
我只对处理最后一个“电子邮件未验证”感兴趣。但是 try 块将捕获所有 ValidationError 异常。鉴于字符串也已翻译,我如何只处理我感兴趣的那个?这样的支票可以吗还是有更好的方法?
if exc.data is _('E-mail is not verified.')
# do stuff
raise exc