Google Firebase 如何捕获特定的身份验证异常错误 - Unity

SHA*_*HAI 1 exception try-catch unity-game-engine firebase firebase-authentication

-如何捕获身份验证异常错误 - Unity -如何捕获用户/电子邮件是否存在 - Unity -在哪里查找身份验证异常错误代码列表 - Unity

*我找到了很多 Android 的答案,所以我决定最终为 Unity 编写解决方案。

SHA*_*HAI 6

答案很简单 - 要么在您想要实现的任务上使用以下函数 -

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;
}
Run Code Online (Sandbox Code Playgroud)

打印的输出Debug.Log(code)是您正在寻找的异常代码。现在您可以对其进行比较 - if (code.Equals("some specific exception...."))并用您的代码完成它。

例子

如何捕获用户/电子邮件是否存在 假设我们注册了一个新用户,并且CreateUserWithEmailAndPasswordAsync想要捕获错误“电子邮件地址已在使用中”我们可以使用我的函数来找出我们需要比较的错误代码和它将打印输出“EmailAlreadyInUse”。接下来我需要做的就是检查if ((code).Equals("EmailAlreadyInUse")) - 另一种可能的方法是在列表中查找错误代码 -

UNITY 的 Auth 异常错误代码列表 所有异常都在类下Firebase.Auth.AuthError,您可以在代码编辑器或 Firebase 网站上的 - Unity - Firebase.Auth - 概述(在 AuthError 下)下看到它们。

希望能帮助到你!