【问题标题】:why the error thrown from callable cloud function does not trigger on failure listener?为什么从可调用云函数引发的错误不会在失败侦听器上触发?
【发布时间】:2020-02-08 05:55:59
【问题描述】:

所以我有这样的可调用云函数:

exports.verifiedUserDeactivateTheEvent = functions.https.onCall(async (data, context) => {

    const eventID = data.eventID
    const verifiedUserID = context.auth.uid

   try {

    // check user
    const verifiedUserDocumentSnapshot = await db.doc(`users/${verifiedUserID}`).get()

    if (!verifiedUserDocumentSnapshot.exists || verifiedUserDocumentSnapshot.data().verified === false) {
        throw new functions.https.HttpsError('Invalid User', 'Invalid User')
    }

    // rest of the code

    return "success"


  } catch(error) {
    console.log(error)
    return error.message
  }



})

如果用户无效或未经授权,则会抛出 httpsError message 'Invalid User'

我在我的 Android 应用程序的可调用函数中使用它

fun verifiedUserDeactivateTheEvent(eventID: String) : Task<String> {

    // Create the arguments to the callable function.
    val data = hashMapOf(
        "eventID" to eventID
    )


    return functions
        .getHttpsCallable("verifiedUserDeactivateTheEvent")
        .call(data)
        .continueWith { task ->
            val result = task.result?.data as String
            result
        }


}

并像这样使用它

verifiedUserDeactivateTheEvent(selectedEvent.eventID).addOnSuccessListener {
    toast(it) // the error message will be shown in here
}.addOnFailureListener {
    toast(it.localizedMessage) // not in here
}

我希望错误消息会显示在onFailureListener 上,但该错误字符串消息仍会显示在onSuccessListener 中。如果存在错误,我需要设置不同的操作

如何使可调用函数抛出的https错误显示在onFailureListener中?

【问题讨论】:

    标签: android firebase google-cloud-functions


    【解决方案1】:

    根据HttpsError 的API 文档,错误代码必须是here 列出的代码之一。您的选择是:

    “好的” | “取消” | “未知” | “无效参数” | “超过最后期限” | “未找到” | “已经存在” | “权限被拒绝” | “资源枯竭” | “失败的前提条件” | “中止” | “超出范围” | “未实现” | “内部” | “不可用” | “数据丢失” | “未经身份验证”

    这些映射到 HTTP 错误响应代码,它决定了客户端将如何处理响应。

    如果是“无效用户”,请尝试使用“permission-denied”:

    throw new functions.https.HttpsError('permission-denied', 'Invalid User')
    

    【讨论】:

    • 先生,对不起,但它似乎仍然没有触发onFailureListener 这是我的android上的logcat i.stack.imgur.com/CNMmq.png,这是使用'permission-denied'后云功能中的错误日志@ 987654324@
    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 2021-09-30
    • 2019-04-12
    • 2020-09-19
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 2016-07-17
    相关资源
    最近更新 更多