简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM