我正在为我的应用程序开发一项功能,其中一个用户可以使用云功能向另一个用户发送通知。我的函数和通知按预期工作,但我无法以正确的方式处理错误,因为我的 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”)。