Firebase Cloud Functions – JavaScript/Typescript Promise reject() issue

Viewed 172

I am using Cloud Functions methods like:

export const someCallableCloudFunction = functions.https.onCall((data, context) => {
   […]
   return new Promise((resolve, reject) => {
      […]
      if (someStatement) {
         resolve("resolveDataString")
      } else {
         reject({ status: "error", code: "429", message: "someErrorMessage" })
      }
   })
})

And calling them with:

firebase.functions().httpsCallable("someCallableCloudFunction")(/*some args*/)
   .then(data => { /* data = "resolveDataString" */ })
   .catch(error => { /* here's the problem */ })

The issue: This is what the error looks like in the client which calls the method: [Error: INTERNAL]. And this is what the error log looks like in the Firebase Cloud Functions Console (Emulator!):

{
  "status": "error",
  "code": "429",
  "message": "Unhandled error",
  "severity": "ERROR"
}

resolve() works perfectly fine but reject() 1) does not print the error object defined in the Cloud Functions method correctly and 2) does not give the error object to the client. Instead the client receives [Error: INTERNAL].

Do I have to use resolve() for both resolve and reject and define statements which can be handled by the client?

1 Answers

As explained in the doc:

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.

and

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.

Related