简体   繁体   中英

How to throw HttpsError in onCall() function and catch on android client

I'm trying to create a stripe charge using a cloud function, and I only receive INTERNAL exception on the client. Where and how should the HttpsError be thrown to be caught by an android client.

I followed the cloud functions documentation found here , but when I came to the error handling section I only retrieve INTERNAL.

Code I have tried:

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
        }
    })

})

Since stripe.charges.create(info); returns a promise i've also tried:

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

My client-side code is basically the same as the documentation.


@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
        }
      }
    }
  }
};

I expect to have the Exception in my paymentCallback that reads the specific details of why it failed, but instead I only get an Internal error everytime.

According to the documentation :

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.

So I would try returning a rejected promise created with Promise.reject(...) rather than throwing an exception out of the catch callback.

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