How can we resolve firebase functions INTERNAL exception in a flutter app?

3,535

Solution 1

You have to catch errors and rethrow them as functions.https.HttpsError. That's nothing new. Literally, everyone's answer in this thread includes this. Hovewer what's I've recently discovered are special defined requirements for error code of HttpsError.

It appears that it can't be any string, but one of the values of FunctionsErrorCode enum. Possible values are listed here:

"ok" | "cancelled" | "unknown" | "invalid-argument" | "deadline-exceeded" | "not-found" | "already-exists" | "permission-denied" | "resource-exhausted" | "failed-precondition" | "aborted" | "out-of-range" | "unimplemented" | "internal" | "unavailable" | "data-loss" | "unauthenticated"

More on: https://firebase.google.com/docs/reference/functions/providers_https_#functionserrorcode

Hope it helps someone, though it's not a new question.

Solution 2

I thought you only meant client side. But to recap what Yadu wrote you should handle it in the cloud function as well. Something like this:

exports.addUser = functions.https.onCall(async (data) => {
  try {
    await admin.firestore().collection("collection_name").add({
      name: "my_name",
      email: "my_email" 
    }
    );
  } catch (err) {
    throw new functions.https.HttpsError('invalid-argument', "some message");
  }
});

and on client side:

HttpsCallable a = FirebaseFunctions.instance.httpsCallable("addUser");

try {
  final x = await a();
  print(x.data);  
} on FirebaseFunctionsException catch (e) {
  // Do clever things with e
} catch (e) {
  // Do other things that might be thrown that I have overlooked
}

You can read a bit more about it on https://firebase.google.com/docs/functions/callable#handle_errors

Client side description is available under the section: "Handle errors on the client"

Share:
3,535
sahil
Author by

sahil

Updated on December 25, 2022

Comments

  • sahil
    sahil over 1 year

    I am working on a flutter app using firebase firestore and firebase functions.I am getting this exception again and again -

    [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [firebase_functions/internal] INTERNAL
    E/flutter (15454): #0      catchPlatformException (package:cloud_functions_platform_interface/src/method_channel/utils/exception.dart:19:3)
    

    I have tried to resolve this exception since last few hours but unable to make any progress. This the code for my function from index.js file.

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp(functions.config().firebase);
    
        exports.addUser = functions.https.onCall(async (data) => {
            await admin.firestore().collection("collection_name").add({
              name: "my_name",
              email: "my_email" 
            }
            );
        });
    

    This is my dart code for flutter app.

                    MyButton(
                      onPressed: () async {
                        HttpsCallable a =
                            FirebaseFunctions.instance.httpsCallable("addUser");
                        final x = await a();
                        print(x.data);  
                      },
                    ),
    

    Thanks in advance !

  • sahil
    sahil over 3 years
    Thanks for your help ,already tried doing it , but the error message is not helpful [firebase_functions/internal] INTERNAL.
  • sahil
    sahil over 3 years
    I have put my dart code in try catch block , and put the index.js code with functions.https.HttpsError , but still I am getting Firebase INTERNAL error. I am using nodjs 8 runtime
  • Robert Sandberg
    Robert Sandberg over 3 years
    I edited my response with a description together with what Yadu meant. If this doesn't work, be sure to post your code where you handle the exceptions.
  • sahil
    sahil over 3 years
    I have now put everything in try catch block , and I am throwing https error in the index.js file. But I think there is some other issue with my code, when I comment out everything except admin.firestore() ,I still get error. Can you give some ideas why this is happening ?