I'm developing a feature for my app, where one user can send a notification to another user using cloud functions. My functions and my notifications work as expected, but I'm not able to handle errors in a proper way, because I always get "INTERNAL" as the error on my Android code.
Here is my code for 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();
}
});
}
Here is where I call 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
}
}
});
And here is my code for my cloud function
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);
});
});
I've tried different versions of code in my catch segment, for example:
.catch( error => {
res.status(500).send('Error');
throw new functions.https.HttpsError('invalid-argument', 'Error');
});
I don't know how am I supposed to make my catch segment, and I really need to get the error I throw in node (not just "INTERNAL").
Callable functions require the following format:
exports.yourFunction = functions.https.onCall((data, context) => {
// ...
});
It is not the same thing as an HTTP triggered Cloud Function, which is what you're using. You can still access it using an HTTP request from your Android app, but if you want to use mFunctions.getHttpsCallable(function).call(data)
, you will need to use a Callable Cloud Function, which I linked above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With