【问题标题】:How to throw HttpsError in onCall() function and catch on android client如何在 onCall() 函数中抛出 HttpsError 并在 android 客户端上捕获
【发布时间】:2019-09-12 21:04:50
【问题描述】:

我正在尝试使用云功能创建条带费用,但我只在客户端收到 INTERNAL 异常。应该在哪里以及如何抛出 HttpsError 以被 android 客户端捕获。

我按照云函数文档找到了here,但是当我来到错误处理部分时,我只检索了 INTERNAL。

我尝试过的代码:

exports.createCharge = functions.https.onCall((data, context) => {
    var amount = data.amount * 100
    var token = data.token
    var info = {
        amount: amount,
        currency: 'usd', 
        description: 'Concession Charge', 
        source: token
    }
    stripe.charges.create(info, (err, charge)=>{
        if(err){
            throw new functions.https.HttpsError(err.type, err.message)
        }else{
            return charge
        }
    })

})

由于stripe.charges.create(info); 返回了一个我也尝试过的承诺:

return stripe.charges.create(info).then((charge) => {
    return charge;
}).catch((error) =>{
    throw new functions.https.HttpsError(err.type, err.message)
})

我的客户端代码和文档基本一致。


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  //......
  confirmBtn.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
      submitPayment("tok_chargeDeclinedInsufficientFunds", 8.50)
        .addOnCompleteListener(paymentCallback);
    }
  }
  //......
}

//Function Call
private Task<String> submitPayment(String token, double amount) {
  Log.d(TAG, "Submitting Payment of: $" + amount);
  // Create the arguments to the callable function.
  Map<String, Object> data = new HashMap<>();
  data.put("token", token);
  data.put("amount", amount);
  return mFunctions
          .getHttpsCallable("createCharge")
          .call(data)
          .continueWith(new Continuation<HttpsCallableResult, String>() {
            @Override
            public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
              String result = (String) task.getResult().getData();
              return result;
             }
           });
}

//Function Complete callback
OnCompleteListener<String> paymentCallback = new OnCompleteListener<String(){
  @Override
  public void onComplete(@NonNull Task<String> task){ 
      if (!task.isSuccessful()){
        Exception e = task.getException();
        if (e instanceof FirebaseFunctionsException) {
          FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
          FirebaseFunctionsException.Code code = ffe.getCode();
          Object details = ffe.getDetails(); //DEBUG BREAKPOINT HERE
        }
      }
    }
  }
};

我希望在 paymentCallback 中出现异常,读取失败原因的具体细节,但我每次都只会收到内部错误。

【问题讨论】:

    标签: javascript java android exception google-cloud-functions


    【解决方案1】:

    根据documentation

    为确保客户端获得有用的错误详细信息,请通过抛出(或返回被拒绝的 Promise)functions.https.HttpsError 实例从可调用对象中返回错误。

    所以我会尝试返回一个使用 Promise.reject(...) 创建的被拒绝的承诺,而不是从 catch 回调中抛出一个异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      相关资源
      最近更新 更多