Unhandled Error gets thrown in Firebase Cloud Functions

Viewed 1096

I'm using Firebase Cloud Functions, and in one of my projects I have a simple function that looks like the following:

exports.responseGiven = functions.https.onCall(async (data, context) => {
  if (!context.auth) {
    throw new functions.https.HttpsError(
        'permission-denied',
        'Must be an user to execute this action'
    );
  }

  const studentPath = data.studentPath;
  const eventPath = data.eventPath;
  const isUpdating = data.isUpdating;

  const studentDoc = await admin.firestore().collection('students').doc('studentPath').get();
  const eventDoc = await studentDoc.ref.collection('entries').doc(eventPath).get();
});

I know where the error is through other methods and why it is, I'm using an invalidStudentPath. But the bigger issue is that the error that gets thrown is this for all of my errors in this project:

Unhandled error function error(...args) {
   write(entryFromArgs('ERROR', args));
} 

How can I get the actual error instead of this obscure message? Thanks for any help.

Update


I have found a workaround for now. Currently I'm wrapping my whole function in a try catch block that looks like this:

exports.responseGiven = functions.https.onCall(async (data, context) => {
  try {
    ...
  } catch (e) {
    console.log('There was an error');
    console.log(e);
  }
});
3 Answers

I just got the same error on a new project. Took me a few hours to realize I forgot to initialize firebase admin after I imported it in my functions

import * as admin from 'firebase-admin';

admin.initializeApp();

I had a case recently where I forgot to await the result of an async (Promise-returning) function inside the relevant try / catch, so it escaped to the outer level. In the code I was working on, the logs look similar in the escaped case and catch error handler case. It is something that evaded my notice during unit testing because the functions were still triggering the node assert rejection hooks with the proper error messages.

Related