【发布时间】:2018-10-28 21:06:49
【问题描述】:
-如何捕获 Auth 异常错误 - Unity - 如果用户/电子邮件存在,如何捕捉 - Unity - 在哪里可以找到 Auth 异常错误代码列表 - Unity
*我为 Android 找到了很多答案,所以我决定最终为 Unity 编写我的解决方案。
【问题讨论】:
标签: firebase unity3d exception firebase-authentication try-catch
-如何捕获 Auth 异常错误 - Unity - 如果用户/电子邮件存在,如何捕捉 - Unity - 在哪里可以找到 Auth 异常错误代码列表 - Unity
*我为 Android 找到了很多答案,所以我决定最终为 Unity 编写我的解决方案。
【问题讨论】:
标签: firebase unity3d exception firebase-authentication try-catch
答案很简单——在你想要完成的任务上使用以下函数——
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; 效果很好