【问题标题】:Getting “INTERNAL” exception when an async Firebase function is called from android app从 android 应用程序调用异步 Firebase 函数时出现“内部”异常
【发布时间】:2018-10-29 23:22:22
【问题描述】:

我正在尝试从 Android 应用调用异步 Firebase 函数,并在函数返回时收到“INTERNAL”异常。

安卓:

 private Task<String> fetchData() {
    // Create the arguments to the callable function, which is just one string
    Map<String, Object> data = new HashMap<>();
    data.put(“id”, “abc”);

    return FirebaseFunctions.getInstance()
            .getHttpsCallable(“calculate”)
            .call(data)
            .continueWith(new Continuation<HttpsCallableResult, String>() {
                @Override
                public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                    Map<String, Object> result = (Map<String, Object>) task.getResult().getData();
                    return (String)result.get(“data”);
                }
            });
 }

Firebase 功能:

exports.calculate = functions.https.onCall((data, context) => {
    const text = data.id;
    return calc.calculate( (err, response) => {
        if(err) {
            // handle error
        } else {
            const data = response.dataValue;
        }
     }).then(() => {
        return {“data”: data};
     });
});

例外:

com.google.firebase.functions.FirebaseFunctionsException: INTERNAL

【问题讨论】:

    标签: android firebase asynchronous google-cloud-functions


    【解决方案1】:

    handling errors in callable functions 的文档表明必须返回 functions.https.HttpsError 的实例:

    为确保客户端获得有用的错误详细信息,请从 可通过抛出(或返回被拒绝的 Promise)调用 functions.https.HttpsError... 的实例 HttpsError 从您的函数中抛出,您的客户端改为接收 消息 INTERNAL 和内部代码出错。

    您的calc.calculate() 调用似乎返回了未正确处理的错误,导致返回的错误状态为 INTERNAL。

    按照上面链接的文档中的示例,您的代码应该是这样的:

    if(err) {
        // handle error
        throw new functions.https.HttpsError('calc-error', 'some error message');
    } else {
        const data = response.dataValue;
    }
    

    【讨论】:

    • 我有同样的问题,它只发生在我从 Android 手机调用我的函数时,但在 Windows 桌面 Chrome 上一切正常。无论如何感谢“functions.https.HttpsError”提示。应该读过文档的那部分。
    • 注意:第一个参数,在上面的例子中,'calc-error',必须是预先批准的错误代码之一。 “计算错误”无效。有效代码列表在这里:firebase.google.com/docs/reference/functions/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2015-06-28
    • 2013-10-04
    • 2018-11-17
    相关资源
    最近更新 更多