【问题标题】:how to show an error from callable cloud function?如何显示可调用云函数的错误?
【发布时间】:2020-02-10 05:09:12
【问题描述】:

来自here中的文档

我可以使用这样的语法抛出错误

throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
      'while authenticated.');

在 Android 设备中

private fun addMessage(text: String): Task<String> {
    // Create the arguments to the callable function.
    val data = hashMapOf(
        "text" to text,
        "push" to true
    )

    return functions
            .getHttpsCallable("addMessage")
            .call(data)
            .continueWith { task ->
                // This continuation runs on either success or failure, but if the task
                // has failed then result will throw an Exception which will be
                // propagated down.
                val result = task.result?.data as String
                result
            }
}

addMessage(inputMessage)
        .addOnCompleteListener(OnCompleteListener { task ->
            if (!task.isSuccessful) {
                val e = task.exception
                if (e is FirebaseFunctionsException) {
                    val code = e.code
                    val details = e.details
                }

                // ...
            }

            // ...
        })

如何获得从云函数抛出的错误消息?我想使用 toast 显示来自云功能的错误消息

该函数必须在认证时调用'+'。

Android 中的语法很奇怪。在细节?但细节是any

【问题讨论】:

    标签: android firebase google-cloud-functions


    【解决方案1】:

    消息"The function must be called while authenticated." 将被设置为本机异常的消息。

    所以要使用它,你会得到e.message的值。

    关于e.details,这是一个可选对象,包含作为第三个参数传递给functions.https.HttpsError 的数据。

    所以对于下面的异常,e.details 将包含缺少的参数名称:

    throw new functions.https.HttpsError('failed-precondition', 'Missing required parameter', 'count');
    

    【讨论】:

      猜你喜欢
      • 2020-07-07
      • 1970-01-01
      • 2020-05-29
      • 2021-08-29
      • 2023-02-04
      • 2020-11-17
      • 2021-04-27
      • 2020-01-02
      相关资源
      最近更新 更多