【问题标题】:Prevent android app crash when callable firebase function throws exception当可调用的firebase函数抛出异常时防止android应用程序崩溃
【发布时间】:2020-07-31 13:52:09
【问题描述】:

我的 Firebase 可调用函数需要在传递无效值时通知客户端。 According to documentation 这应该使用 functions.https.HttpsError-

      if (!condition) {
        throw new functions.https.HttpsError(
          'invalid-argument',
          'Cheating will incur ban'
        );
      }

添加客户端代码以given in the docs 调用函数会导致应用崩溃。

    fun addPlayTime(playTime: Int): Task<String> {
        val data = hashMapOf(
            "playTime" to playTime
        )
        return functions
            .getHttpsCallable("addPlayTime")
            .call(data)
            .continueWith { task ->
                val result = task.result?.data as String
                result
            }
    }
            viewModel.addPlayTime(10000)
                .addOnCompleteListener { task ->
                    Timber.d("API response: ${task.result}")
                    if (!task.isSuccessful) {
                        val e = task.exception
                        if (e is FirebaseFunctionsException) {
                            val code = e.code
                            val details = e.details
                            Timber.d("API call failed: $details")
                        }
                    }
                }

我可以在 logcat 中看到错误。如何在我的应用程序不崩溃的情况下处理此异常?在 try-catch 中包装上面的代码没有帮助。

2020-07-31 19:04:28.722 17502-17502/com.teamvanar.gcharge E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.teamvanar.gcharge, PID: 17502
    com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.functions.FirebaseFunctionsException: Cheating will incur ban
        at com.google.android.gms.tasks.zzu.getResult(Unknown Source:15)
        at com.teamvanar.gcharge.MainActivity$onCreate$2.onComplete(MainActivity.kt:67)
        at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: com.google.firebase.functions.FirebaseFunctionsException: Cheating will incur ban

【问题讨论】:

    标签: android firebase kotlin google-cloud-functions


    【解决方案1】:

    根据我的经验,Firebase Http Callable 在 Kotlin 中使用 CompleteListener 时存在一些类型转换(反射?)问题,并且这些 FATAL 无法通过 try-catch 捕获。

    SuccessListenerFailureListener 替换它,在我的情况下解决这个问题:

    // from this
    someFirebaseFunction.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            task.result?.let { /* do something */ }
        } else {
            println("Error: ${task.exception}")
        }
    }
    
    // to this
    someFirebaseFunction.addOnSuccessListener { result ->
        /* do something */
    }.addOnFailureListener { exception ->
            println("Error: $exception")
        }
    }
    

    在你的情况下,这可能有效:

    viewModel.addPlayTime(10000).addOnSuccessListener { result ->
        Timber.d("API response: $result")
    }.addOnFailureListener { e ->
        if (e is FirebaseFunctionsException) {
            val code = e.code
            val details = e.details
            Timber.d("API call failed: $details")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      • 2014-05-23
      相关资源
      最近更新 更多