Google Firebase how to catch specific Auth exception errors - Unity

后端 未结 1 1098
一生所求
一生所求 2020-12-20 06:42

-How to catch Auth exception errors - Unity -How to catch if user/email exists - Unity -Where to find the list of Auth exceptions error code - Unity

*I found a lot o

相关标签:
1条回答
  • 2020-12-20 06:57

    The answer is simple- either use the following function on the task you are trying to achieve -

    protected bool LogTaskCompletion(Task task, string operation)
    {
        bool complete = false;
        if (task.IsCanceled)
        {
            Debug.Log(operation + " canceled.");
        }
        else if (task.IsFaulted)
        {
            Debug.Log(operation + " encounted an error.");
            foreach (Exception exception in task.Exception.Flatten().InnerExceptions)
            {
                string authErrorCode = "";
                Firebase.FirebaseException firebaseEx = exception as Firebase.FirebaseException;
                if (firebaseEx != null)
                {
                    authErrorCode = String.Format("AuthError.{0}: ",
                      ((Firebase.Auth.AuthError)firebaseEx.ErrorCode).ToString());
                }
                Debug.Log("number- "+ authErrorCode +"the exception is- "+ exception.ToString());
                string code = ((Firebase.Auth.AuthError)firebaseEx.ErrorCode).ToString();
                Debug.Log(code);
            }
        }
        else if (task.IsCompleted)
        {
            Debug.Log(operation + " completed");
            complete = true;
        }
        return complete;
    }
    

    The printed output Debug.Log(code) is the exception code you are looking for. Now you can compare it - if (code.Equals("some specific exception....")) and complete it with your code.

    Example:

    How to catch if user/email exists Let's say we sign up a new user with CreateUserWithEmailAndPasswordAsync and we want to catch the error " The email address is already in use" We can use my function to find out what the error code we need to compare and it will print to output "EmailAlreadyInUse". Next all I need to do is to check if ((code).Equals("EmailAlreadyInUse")) -Another possible way is to find the error code in a list-

    List of Auth exceptions error code FOR UNITY All the exceptions are under class Firebase.Auth.AuthError you can see them either on your code editor, or on Firebase website under - Unity - Firebase.Auth - Overview (under AuthError).

    Hope it helps!

    0 讨论(0)
提交回复
热议问题