【问题标题】:Google Firebase Auth in Unity: How to read error codesUnity 中的 Google Firebase 身份验证:如何读取错误代码
【发布时间】:2023-03-18 00:39:01
【问题描述】:

在 Unity 中使用 Google Firebase 身份验证插件时,如何读取错误请求的错误代码?

例如,在这段代码中:

auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
        if(task.IsFaulted){
            Debug.Log("ERROR ENCOUNTERED: " + task.Exception);
            return;
        }

        if(task.IsCompleted){
            // Success!
        }
    });

您可以看到,如果发生错误,我可以将异常记录出来,它会打印以下内容:

遇到错误:System.AggregateException:引发了“System.AggregateException”类型的异常。

Firebase.FirebaseException:没有与此标识符对应的用户记录。该用户可能已被删除。

这是非常可读的,但放在 switch 语句中不是很优雅。我有什么办法可以将 task.Exception 转换为 FirebaseException 以便我可以获取错误代码?在某处是否有这些错误代码的列表?我可以找到 FirebaseException 的文档,但没有错误代码。感谢您的帮助!

编辑:

因此,虽然我仍然希望得到答案,但我开始认为 Google 希望开发人员根据请求的上下文使用一揽子错误语句。例如,当使用电子邮件和密码登录失败时(如上面的代码),我们应该使用“电子邮件或密码不正确”的常见说法。这样做的问题是,我无法让用户知道他们提供不正确的详细信息与他们输入根本没有关联帐户的电子邮件之间的区别。

【问题讨论】:

标签: c# firebase unity3d firebase-authentication


【解决方案1】:

我遇到了同样的困境,试图找出我从 Firebase 获得的错误代码并为用户显示适当的消息。

读取 Firebase 异常的正确方法是使用我创建的这个函数:

bool CheckError(AggregateException exception, int firebaseExceptionCode)
    {
        Firebase.FirebaseException fbEx = null;
        foreach (Exception e in exception.Flatten().InnerExceptions)
        {
        fbEx = e as Firebase.FirebaseException;
        if (fbEx != null)
            break;
    }

    if (fbEx != null)
    {
        if (fbEx.ErrorCode == firebaseExceptionCode)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    return false;
}

你可以这样使用它:

auth.SignInWithEmailAndPasswordAsync("test@gmail.com", "password").ContinueWith(task => {
        if (task.IsCanceled)
        {
            Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
            return;
        }
        if (task.IsFaulted)
        {
            if(CheckError(task.Exception, (int)Firebase.Auth.AuthError.EmailAlreadyInUse))
            {
                // do whatever you want in this case
                Debug.LogError("Email already in use");
            }
            Debug.LogError("UpdateEmailAsync encountered an error: " + task.Exception);
        }
    }

以下是来自 firebase 的更多代码示例: https://github.com/firebase/quickstart-unity/blob/master/auth/testapp/Assets/Firebase/Sample/Auth/UIHandler.cs

我从这个帖子得到了答案: https://github.com/firebase/quickstart-unity/issues/96

我希望这会对某人有所帮助。万事如意!

【讨论】:

  • 您的代码看起来很像other answer 中的代码。您的答案究竟有何不同/更好?
  • 我尝试从这里实现另一个答案,但它没有用。不同之处在于这一行:exception.Flatten().InnerExceptions。您也可以有 Auth 异常的用例,从这里获取它们:(int)Firebase.Auth.AuthError.*
  • fbEx = e as Firebase.FirebaseException; 替换为fbEx = e.GetBaseException() as FirebaseException; 即可完美运行
【解决方案2】:

希望您现在已经解决了这个问题,但我刚刚遇到了完全相同的问题,我将分享我的解决方案:

根据MSDN,System.AggregateException 表示任务执行期间可能发生的一个或多个错误。

因此,您需要遍历 AggregateException 呈现的 InnerException,并查找可疑的 FirebaseException:

检索 FirebaseException:

AggregateException ex = task.Exception as AggregateException;
if (ex != null) {
  Firebase.FirebaseException fbEx = null;
  foreach (Exception e in ex.InnerExceptions) {
    fbEx = e as Firebase.FirebaseException;
    if (fbEx != null)
      break;
  }

  if (fbEx != null) {
    Debug.LogError("Encountered a FirebaseException:" + fbEx.Message);
  }
}

获取错误代码:

希望我能在这里提供帮助,但我没有找到任何东西 - 这些都没有记录在官方 API、AFIK 中。唯一参考说明:"If the error code is 0, the error is with the Task itself, and not the API. See the exception message for more detail."

【讨论】:

    猜你喜欢
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    相关资源
    最近更新 更多