Agung
Agung

Reputation: 13803

can I get firestore error code when using Source.Server but user is not connected to internet?

I have an error message like this if the user is not connected to internet, I set the source to be 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.)

I want to write my own error message, I can actually hard code the message, something like this

if (message == "failed to get document from server ........") {
   showToast("showing my own message here")
}

but it can easily break, if for example, firebase sdk change the error message string. is there any error code or enum error to identify this ?

here is the code

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
        }


    }

Upvotes: 0

Views: 1152

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

The exception delivered to your failure listener callback is of type FirebaseFirestoreException. That class contains a formal Code type that determines the exact error. I can't tell which one it would be, but I best you could figure out by examining the results. If there isn't actually a code for some reason, try filing an issue on the Firebase SDK GitHub.

Upvotes: 2

Related Questions