Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting “INTERNAL” exception when an async Firebase function is called from android app

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
like image 757
Maya Avatar asked May 19 '18 21:05

Maya


People also ask

Are firebase functions async?

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.

What is the difference between onCall http callable and onRequest HTTP request functions?

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.

What is HttpsCallable?

An HttpsCallable is a reference to a "callable" http trigger in Google Cloud Functions.


1 Answers

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 than HttpsError 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;
}
like image 180
Bob Snyder Avatar answered Oct 09 '22 00:10

Bob Snyder