【发布时间】:2020-01-22 00:32:06
【问题描述】:
场景:
当出现异常时需要知道错误的类型。我找不到合适的参考。
来源:
代码
从数据库中获取原始值
string _error = string.Empty;
await RootDB.Child(_node).GetValueAsync()
.ContinueWith(
(_task) =>
{
if (_task.Exception != null)
{
Debug.Log("Exception Found in " + _node + " | " + _task.Exception.Message);
_error = CloudErrorHandle.DatabaseErrorMessageFor(_task.Exception.InnerExceptions);
}
else if (_task.IsCanceled == true)
{
Debug.Log(_node + " | is canceled ");
}
else if (_task.IsFaulted == true)
{
Debug.Log(_node + " | is Faulted ");
}
else if (_task.IsCompleted == true)
{
Debug.Log(_node + "| is Completed");
DataSnapshot _snapShot = _task.Result;
if (_snapShot.Exists == true)
{
// Get the value _snapShot.GetRawJsonValue();
}
else
{
Debug.Log("Snapshot DOES NOT Exist for " + _node);
}
}
});
// 来自 CloudErrorHandle
internal static string DatabaseErrorMessageFor(System.Collections.ObjectModel.ReadOnlyCollection<System.Exception> _innerExceptions)
{//https://stackoverflow.com/questions/53036083/google-firebase-how-to-catch-specific-auth-exception-errors-unity
string _errorMessage = "None";
for (int _i = 0; _i < _innerExceptions.Count; _i++)
{
FirebaseException _firebaseEx = _innerExceptions[_i] as FirebaseException;
_errorMessage = GetDatabaseExceptionForThisID(_firebaseEx.ErrorCode);
}
return _errorMessage;
}
internal static string GetDatabaseExceptionForThisID(int _identification)
{ //https://firebase.google.com/docs/reference/unity/class/firebase/database/database-error
string _error = "Unknown";
switch(_identification)
{
case -4:
_error = "Disconnected";
break;
case -6:
_error = "ExpiredToken";
break;
case -7:
_error = "InvalidToken";
break;
case -8:
_error = "MaxRetries";
break;
case -24:
_error = "NetworkError";
break;
case -2:
_error = "OperationFailed";
break;
case -9:
_error = "OverriddenBySet";
break;
case -3:
_error = "PermissionDenied";
break;
case -10:
_error = "Unavailable";
break;
case -999:
_error = "UnknownError";
break;
case -11:
_error = "UserCodeException";
break;
case -25:
_error = "WriteCanceled";
break;
}
return _error;
}
请求
- 出现异常时获取错误的方法正确吗?
- 在迭代“InnerExceptions”时会出现多个错误吗?
- 我了解到,当与 RL 数据库交互时出现任何问题时,异常将永远为 null
谢谢
【问题讨论】:
-
你为什么要把
async-await和ContinueWith混在一起? -
你是在说
await RootDB.Child(_node).GetValueAsync()如果那是你所指的,那么我必须等待从该节点获取值。请纠正我哪里错了。谢谢你。需要明确的是,该类不继承 Monobehavior。
标签: c# firebase unity3d firebase-realtime-database async-await