Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity中的Google Firebase身份验证:如何读取错误代码_C#_Firebase_Unity3d_Firebase Authentication - Fatal编程技术网

C# Unity中的Google Firebase身份验证:如何读取错误代码

C# Unity中的Google Firebase身份验证:如何读取错误代码,c#,firebase,unity3d,firebase-authentication,C#,Firebase,Unity3d,Firebase Authentication,在Unity中使用Google Firebase身份验证插件时,如何读取错误请求的错误代码 例如,在此代码中: auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => { if(task.IsFaulted){ Debug.Log("ERROR ENCOUNTERED: " + task.Exception); return;

在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的文档,但是错误代码不在那里。谢谢你的帮助

编辑:

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

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

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

根据,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);
  }
}
获取错误代码:

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中记录。唯一的参考说明是:

我在试图找出从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的更多代码示例:

我从这条线索中得到了答案:


我希望这能帮助别人。祝你一切顺利

这里有一个解决方案!您的代码看起来确实非常像中的代码。你的答案到底有什么不同/更好的地方?我尝试从这里实现另一个答案,但没有成功。区别在于这一行:exception.flatte().InnerExceptions。您还可以从这里获得Auth异常的用例:(int)Firebase.Auth.AuthError.*将
fbEx=e替换为Firebase.FirebaseException
by
fbEx=e.GetBaseException()作为FirebaseException然后它就完美地工作了