首页 > 解决方案 > 从 android 应用程序调用异步 Firebase 函数时出现“内部”异常

问题描述

我正在尝试从 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”);
                }
            });
 }

火力基地功能:

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

标签: androidfirebaseasynchronousgoogle-cloud-functions

解决方案


处理可调用函数中的错误的文档表明必须返回functions.https.HttpsError的实例:

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

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

按照上面链接的文档中的示例,您的代码应类似于:

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

推荐阅读