Search code examples
pythondjangopython-social-auth

extracting email address from exception message


In python-social-auth, when a user registers with an existing email address, an exception is raised.

I managed to catch this exception:

msg =   "%s" % exception

If I print msg, I get the following output:

(1062, "Duplicate entry 'email@id.com' for key 'email_UNIQUE'")

I would like to detect this exception and display a custom error message to the user, something like:

This email id already exists please login with email@id.com or choose to sign in.

So my question is, how do I get only the email address from the exception message? Or is there any shortcut like {{ exception.email }} to get the email address in the template?


Solution

  • use regular expression

    import re
    
    strs="""(1062, "Duplicate entry 'email@id.com' for key 'email_UNIQUE'")"""
    
    email_id =re.findall(r"'(.*?)'", strs, re.DOTALL)[0]
    print 'This email id already exists please login with {} or choose to sign in.'.format(email_id)
    #output This email id already exists please login with email@id.com or choose to sign in.