繁体   English   中英

防止 android 可调用时应用程序崩溃 firebase function 抛出异常

[英]Prevent android app crash when callable firebase function throws exception

我的 Firebase 可调用 function 需要在传递无效值时通知客户端。 根据文档,这应该使用functions.https.HttpsError来完成 -

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

添加客户端代码以调用文档中给出的 function 会导致应用程序崩溃。

    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

根据我的经验,Firebase Http Callable 在 Kotlin 中使用CompleteListener时存在一些类型转换(反射?)问题,而这些致命错误无法被 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")
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM