Firebase 云函数错误代码和错误消息在 Android 上始终为内部

2024-03-11

我正在为我的应用程序开发一项功能,其中一个用户可以使用云功能向另一个用户发送通知。我的函数和通知按预期工作,但我无法以正确的方式处理错误,因为我的 Android 代码上总是出现“INTERNAL”错误。

这是我的 Android 代码:

public static Task<String> callFirebaseFunction(HashMap<String, Object> data, String funcion){

    FirebaseFunctions mFunctions = FirebaseFunctions.getInstance();

    return mFunctions
        .getHttpsCallable(funcion)
        .call(data)
        .continueWith(new Continuation<HttpsCallableResult, String>() {
            @Override
            public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                return (String) task.getResult().getData();
            }
        });
}

这是我打电话的地方调用Firebase函数

Utilities.callFirebaseFunction(dataNoty, nombreFuncion)
    .addOnCompleteListener(new OnCompleteListener<String>() {
        @Override
        public void onComplete(@NonNull Task<String> task) {
            if ( !task.isSuccessful() ){
                Exception e = task.getException();
                if (e instanceof FirebaseFunctionsException) {
                    FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
                    FirebaseFunctionsException.Code code = ffe.getCode(); // It's always INTERNAL
                    Object details = ffe.getMessage(); // It's always INTERNAL
                } else {
                    // handle error
                }
            } else {
                // success
            }
        }
    });

这是我的云函数代码

exports.helloWorld = functions.https.onRequest((req, res) => {
    let data     = req.body.data;
    let id = data['id'].toString();
    db.collection('users').doc(id).get()
        .then((snapshot) => {
            let infoUser = snapshot.data();
            // Verifies data
            if ( typeof infoUser !== "undefined" && Object.keys(infoUser).length > 0 ){
                // Some code
            } else {
                throw new functions.https.HttpsError(
                    'invalid-argument',             // code
                    'Error',                        // message
                    'User ' + id + ' not found'     // details
                );
            }
        }).then( () => {
            console.log('Success');
            return res.status(200).send("success");
        }).catch( error => {
            res.status(500).send(error.details);
            return new functions.https.HttpsError(error.code, error.details);
        });
});

我在 catch 段中尝试了不同版本的代码,例如:

.catch( error => {
    res.status(500).send('Error');
    throw new functions.https.HttpsError('invalid-argument', 'Error');
});

我不知道我应该如何制作我的 catch 段,而且我真的需要得到我在节点中抛出的错误(而不仅仅是“INTERNAL”)。


可调用函数 https://firebase.google.com/docs/functions/callable需要以下格式:

exports.yourFunction = functions.https.onCall((data, context) => {
  // ...
});

它与HTTP触发云函数 https://firebase.google.com/docs/functions/http-events,这就是你正在使用的。您仍然可以使用 Android 应用程序中的 HTTP 请求来访问它,但如果您想使用mFunctions.getHttpsCallable(function).call(data),您将需要使用我上面链接的可调用云函数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Firebase 云函数错误代码和错误消息在 Android 上始终为内部 的相关文章

随机推荐