【发布时间】: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