如何从Firestore Task获取响应代码

时间:2018-01-13 19:40:22

标签: android exception google-cloud-firestore

com.google.android.gms.tasks我想知道有哪些类型的异常以及如何在错误响应中过滤它们?

这表明了我想看到的内容:

   db.collection("users")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                     if (task.isSuccessful()) {
                         for (DocumentSnapshot document : task.getResult()) {
                             s(document.getId() + " => " + document.getData());
                         }
                     } else {
// Demonstrating here what I like to see using the okhttp respons code as en example.. 
                         switch (task.getException().getCause()) {// obviously not working only for demonstration.. 
                            case "OK":
                               return Status.OK;
                            case "ZERO_RESULTS":
                               return Status.ZERO_RESULTS;
                            case "OVER_QUERY_LIMIT":
                               return Status.OVER_QUERY_LIMIT;
                            case "REQUEST_DENIED":
                               return Status.REQUEST_DENIED;
                            case "INVALID_REQUEST":
                               return Status.INVALID_REQUEST;
                            default:
                               return null;
                         }
                     }
                 }
             });

如果可能的话会很棒。

1 个答案:

答案 0 :(得分:2)

我自己没有尝试过,但是通过参考文档,您应该可以致电FirebaseFirestoreException.getCode()

def num_digits(n):
    count = 0
    while n>0:
      count+=1
      n=n//10
    return count

print(num_digits(2))
print(num_digits(12))
print(num_digits(123))

这为您提供了此处记录的代码之一:https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/FirebaseFirestoreException.Code