实时数据库错误检索-Unity3D

时间:2020-01-22 00:32:06

标签: c# firebase unity3d firebase-realtime-database async-await

场景

需要在出现异常时知道错误的类型。我找不到合适的参考。

来源

代码

从数据库中获取原始价值

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

请求

  1. 有异常时获取错误的正确方法吗?
  2. 正在迭代“ InnerExceptions”时会出现多个错误吗?
  3. 我的理解是,当与RL数据库交互时出现任何问题时,该异常从不为空

谢谢

1 个答案:

答案 0 :(得分:1)

如果我对您的问题的理解是正确的,则您的代码应如下所示:

var _error = null;
var child = RootDB.Child(_node);

try
{
    var result = await child.GetValueAsync();

    Debug.Log(_node + "| is Completed");
    DataSnapshot _snapShot = result;
    if (_snapShot.Exists == true)
    {
        // Get the value _snapShot.GetRawJsonValue();
    }
    else
    {
        Debug.Log("Snapshot DOES NOT Exist for " + _node);
    }

}
catch (OperationCanceledException ex)
{
    Debug.Log(_node + " | is canceled ");
}
catch (Exception ex)
{
    Debug.Log("Exception Found in " + _node + " | " + ex.Message);
    _error = CloudErrorHandle.DatabaseErrorMessageFor(ex.InnerExceptions);
}