I’m trying to call an async Firebase function from android app and getting “INTERNAL” exception when the function returns.
Android:
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”);
}
});
}
Firebase Function:
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};
});
});
Exception:
com.google.firebase.functions.FirebaseFunctionsException: INTERNAL
Firebase APIs are sensitive to the performance of your app's main thread. This means that any Firebase API that needs to deal with data on disk or network is implemented in an asynchronous style. The functions will return immediately , so you can call them on the main thread without worrying about performance.
onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.
An HttpsCallable is a reference to a "callable" http trigger in Google Cloud Functions.
The documentation for handling errors in callable functions indicates that an instance of functions.https.HttpsError must be returned:
To ensure the client gets useful error details, return errors from a callable by throwing (or returning a Promise rejected with) an instance of
functions.https.HttpsError
... If an error other thanHttpsError
is thrown from your functions, your client instead receives an error with the message INTERNAL and the code internal.
It seems likely that your calc.calculate()
call is returning an error that is not being handled correctly, resulting in a returned error status of INTERNAL.
Following the example in the document linked above, your code should be something like:
if(err) {
// handle error
throw new functions.https.HttpsError('calc-error', 'some error message');
} else {
const data = response.dataValue;
}
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