【问题标题】:Firebase cloud function error code and error message is always Internal on AndroidFirebase 云功能错误代码和错误消息在 Android 上始终为 Internal
【发布时间】:2019-01-13 22:49:27
【问题描述】:

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

这是我的 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();
            }
        });
}

这里是我调用 callFirebaseFunction

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 段,我真的需要得到我在节点中抛出的错误(不仅仅是“内部”)。

【问题讨论】:

  • 它发送状态标头HTTP 500,这是一个internal server error ...也许用return new functions.https.HttpsError()替换throw?错误代码必须是一个整数......例如。 HTTP 400HTTP 422.
  • 我试过返回和相同的结果。错误码必须是字符串,这里是HttpsError的文档firebase.google.com/docs/reference/functions/…
  • 确实如此。但是,这些字符串用于常量,它们被映射回常见的 HTTP 错误代码:github.com/googleapis/googleapis/blob/master/google/rpc/… ...正确的错误代码将是 UNAUTHENTICATED... 这意味着 res.status(401)。可以通过两种不同的方法通知错误(其中一种就足够了,您总是两种方法都有)。

标签: java android node.js firebase google-cloud-functions


【解决方案1】:

Callable functions 需要以下格式:

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

它与您正在使用的HTTP triggered Cloud Function 不同。您仍然可以使用来自 Android 应用程序的 HTTP 请求访问它,但如果您想使用 mFunctions.getHttpsCallable(function).call(data),则需要使用我在上面链接的 Callable Cloud Function。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-12
    • 2018-11-17
    • 2018-11-27
    • 2017-10-09
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2018-11-25
    相关资源
    最近更新 更多