【问题标题】:Google Firebase how to catch specific Auth exception errors - UnityGoogle Firebase 如何捕获特定的身份验证异常错误 - Unity
【发布时间】:2018-10-28 21:06:49
【问题描述】:

-如何捕获 Auth 异常错误 - Unity - 如果用户/电子邮件存在,如何捕捉 - Unity - 在哪里可以找到 Auth 异常错误代码列表 - Unity

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

【问题讨论】:

    标签: firebase unity3d exception firebase-authentication try-catch


    【解决方案1】:

    答案很简单——在你想要完成的任务上使用以下函数——

    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;
    }
    

    打印输出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 下)下查看它们。

    希望对你有帮助!

    【讨论】:

    • 就我而言,使用fbEx = e.GetBaseException() as FirebaseException; 代替Firebase.FirebaseException firebaseEx = exception as Firebase.FirebaseException; 效果很好
    • 非常感谢!回答了我的问题并教会了我一些关于异常的知识。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2017-07-05
    • 2023-03-18
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多