Firebase Functions: Unclear "connection error"

15,664

Solution 1

Function execution took ****ms, finished with status: 'connection error' has two main causes:

  1. Trying to call a non-Google service while on the Spark plan (see pricing page). This can be fixed by upgrading to a paid plan.

  1. A function doesn’t know whether a promise resolved or not. Every promise must be returned, as mentioned in the docs here. There is also a blog post (with helpful video!) about this.

A couple of examples of unreturned promises:

exports.someFunc = functions.database.ref('/some/path').onCreate(event => {
     let db = admin.database();

     // UNRETURNED PROMISE
     db.ref('/some/path').remove();

     return db.ref('/some/other/path').set(event.data.val());
});
exports.makeUppercase = functions.database.ref('/hello/{pushId}').onWrite(event => {
    return event.data.ref.set('world').then(snap => {

      // UNRETURNED PROMISE
      admin.database().ref('lastwrite').set(admin.database.ServerValue.TIMESTAMP);

    });
});
exports.makeUppercase = functions.database.ref('/hello/{pushId}').onWrite(event => {

    // UNRETURNED PROMISE
    event.data.ref.set('world').then(snap => {
        return admin.database().ref('lastwrite').set(admin.database.ServerValue.TIMESTAMP);
    });
});

To help catch this mistake before deploying code, check out this eslint rule.

For an in-depth look at promises, here are some helpful resources:

Solution 2

Even though this question has an approved answer, you may have followed the steps in that answer and still reached a point where the error was still occurring.

In that case, we were informed by GCP that there's a known issue with Node 8 CFs and this connection error, for which the workaround is to update the node version to 10.

Related github issue: https://github.com/firebase/firebase-functions/issues/429

Specific comment: https://github.com/firebase/firebase-functions/issues/429#issuecomment-577324193

Solution 3

I faced the same issue while deploying uninstallTracking event to firebase for android device,

Turns out that the property I was trying to access was available for only some users , So when it couldn't find the property for those other users it gives this error

So first just check the property you are trying to access is there or not

Solution 4

I think it might be too many simultaneous firebase database connections :/ https://groups.google.com/forum/#!topic/firebase-talk/4RjyYIDqMVQ

Share:
15,664
Kyle Hotchkiss
Author by

Kyle Hotchkiss

Updated on June 08, 2022

Comments

  • Kyle Hotchkiss
    Kyle Hotchkiss about 2 years

    I am getting this error every so many runs with my HTTP Firebase Cloud Function:

    Function execution took ****ms, finished with status: 'connection error'

    It happens inconsistently but I can't quite narrow down what the problem is. I don't believe the error is in my app as it's not showing an error printout. And my own connection with firebase while running this cloud function isn't cutting out.

    Any ideas why Firebase randomly fails cloud function executions with "connection error"?