简体   繁体   English

Firebase 云函数 – JavaScript/Typescript Promise reject() 问题

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

I am using Cloud Functions methods like:我正在使用 Cloud Functions 方法,例如:

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] .问题:这是调用方法的客户端中的[Error: INTERNAL] And this is what the error log looks like in the Firebase Cloud Functions Console (Emulator!):这就是 Firebase Cloud Functions 控制台(模拟器!)中错误日志的样子:

{
  "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. resolve()工作得很好,但reject() 1) 没有正确打印 Cloud Functions 方法中定义的错误对象,并且 2) 没有将错误对象提供给客户端。 Instead the client receives [Error: INTERNAL] .相反,客户端会收到[Error: INTERNAL]

Do I have to use resolve() for both resolve and reject and define statements which can be handled by the client?我是否必须将resolve()用于解析和拒绝以及定义可由客户端处理的语句?

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 .为确保客户端获得有用的错误详细信息,请通过抛出(或返回被拒绝的 Promise )一个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.如果您的函数抛出 HttpsError 以外的错误,则您的客户端会收到带有消息 INTERNAL和内部代码的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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