android - 使用Source.Server但用户未连接到互联网时,如何获取Firestore错误代码?

标签 android firebase kotlin google-cloud-firestore

如果用户未连接到互联网,我会收到这样的错误消息,我将源设置为Source.Server:

failed to get document from server. (However, this document does exist in the local cache. Run again without setting to SERVER to retrieve the cached document.)



我想编写自己的错误消息,实际上我可以对消息进行硬编码,像这样
if (message == "failed to get document from server ........") {
   showToast("showing my own message here")
}

但是它很容易中断,例如,如果firebase sdk更改错误消息字符串。是否有任何错误代码或枚举错误可识别此错误?

这是代码
fun getUserData(source: Source = Source.SERVER, userID: String, completion: (user: User?, errorMessage:String?) -> Unit) {

        db.document("users/${userID}").get(source).addOnSuccessListener { documentSnapshot ->

            if (documentSnapshot == null || !documentSnapshot.exists()) {
                completion(null,"Account is not found")
                return@addOnSuccessListener
            }

            val userData = documentSnapshot.data ?: return@addOnSuccessListener
            val user = User(userData)
            completion(user,null)


        }.addOnFailureListener {
            completion(null,it.localizedMessage) // error message comes from here
        }


    }

最佳答案

传递给故障侦听器回调的异常的类型为FirebaseFirestoreException。该类包含一个确定确切错误的正式Code类型。我不知道会是哪一种,但是我最好您可以通过检查结果来弄清楚。如果实际上由于某种原因没有代码,请尝试在the Firebase SDK GitHub上提交问题。

关于android - 使用Source.Server但用户未连接到互联网时,如何获取Firestore错误代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59691799/

相关文章:

android - Android : INVALID_SCOPE 无法使用 Google+ 登录获取授权码

android - 我应该使用 J Center 的 Guava-base-libraries 还是有其他地方可以获得模块化的 Guava-jars?

android - 单选按钮的自定义图标

android - 为什么带有自定义适配器的 ListView 有时会表现得很奇怪?

java - Firebase Recycler View 不显示任何内容

java - 在另一台设备上使用 EasyCrypt 库完成密码时出错

ios - 如何从Firebase以值的降序获取数据?

android - Kotlin 协程使用 runBlocking 进行单元测试不等待执行

kotlin - Kotlin中的RxJava2使用Retrofit-Vertx IllegalStateException消息== null

kotlin - 在 lambda 表达式中使用可完成的 rxjava2 时的显式返回类型