![](/img/trans.png)
[英]UNAUTHENTICATED ERROR : Firebase function called from android app
[英]Getting “INTERNAL” exception when an async Firebase function is called from android app
我正在嘗試從 android 應用程序調用異步 Firebase function,並在 function 返回時出現“內部”異常。
Android:
private Task<String> fetchData() {
// Create the arguments to the callable function, which is just one string
Map<String, Object> data = new HashMap<>();
data.put(“id”, “abc”);
return FirebaseFunctions.getInstance()
.getHttpsCallable(“calculate”)
.call(data)
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
Map<String, Object> result = (Map<String, Object>) task.getResult().getData();
return (String)result.get(“data”);
}
});
}
Firebase Function:
exports.calculate = functions.https.onCall((data, context) => {
const text = data.id;
return calc.calculate( (err, response) => {
if(err) {
// handle error
} else {
const data = response.dataValue;
}
}).then(() => {
return {“data”: data};
});
});
例外:
com.google.firebase.functions.FirebaseFunctionsException: INTERNAL
處理可調用函數中的錯誤的文檔指出必須返回一個函數實例。https.HttpsError:
為了確保客戶端獲得有用的錯誤詳細信息,請通過拋出(或返回被拒絕的Promise)
functions.https.HttpsError
實例從可調用functions.https.HttpsError
返回錯誤HttpsError
...如果從函數中拋出了HttpsError
以外的錯誤,HttpsError
客戶端代替收到錯誤消息INTERNAL和內部代碼。
您的calc.calculate()
調用似乎返回了未正確處理的錯誤,導致返回的錯誤狀態為INTERNAL。
按照上面鏈接的文檔中的示例,您的代碼應類似於:
if(err) {
// handle error
throw new functions.https.HttpsError('calc-error', 'some error message');
} else {
const data = response.dataValue;
}
當您調用 httpCallable 時,您將得到一個名為FirebaseFunctionsExceptions的異常。 您必須處理此異常。 用try和catch包裝你的代碼。
例子:-
try {
final result = await FirebaseFunctions.instance
.httpsCallable('deleteUser')
.call({});
} on FirebaseFunctionsException catch (error) {
print(error.message);
}
有關更多信息,請點擊此鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.