[英]Getting “INTERNAL” exception when an async Firebase function is called from android app
I'm trying to call an async Firebase function from android app and getting “INTERNAL” exception when the function returns.我正在尝试从 android 应用程序调用异步 Firebase function,并在 function 返回时出现“内部”异常。
Android: 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: 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};
});
});
Exception:例外:
com.google.firebase.functions.FirebaseFunctionsException: INTERNAL
The documentation for handling errors in callable functions indicates that an instance of functions.https.HttpsError must be returned: 处理可调用函数中的错误的文档指出必须返回一个函数实例。https.HttpsError:
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
... If an error other thanHttpsError
is thrown from your functions, your client instead receives an error with the message INTERNAL and the code internal. 为了确保客户端获得有用的错误详细信息,请通过抛出(或返回被拒绝的Promise)functions.https.HttpsError
实例从可调用functions.https.HttpsError
返回错误HttpsError
...如果从函数中抛出了HttpsError
以外的错误,HttpsError
客户端代替收到错误消息INTERNAL和内部代码。
It seems likely that your calc.calculate()
call is returning an error that is not being handled correctly, resulting in a returned error status of INTERNAL. 您的calc.calculate()
调用似乎返回了未正确处理的错误,导致返回的错误状态为INTERNAL。
Following the example in the document linked above, your code should be something like: 按照上面链接的文档中的示例,您的代码应类似于:
if(err) {
// handle error
throw new functions.https.HttpsError('calc-error', 'some error message');
} else {
const data = response.dataValue;
}
When you call httpCallable,you will get an exception called FirebaseFunctionsExceptions .当您调用 httpCallable 时,您将得到一个名为FirebaseFunctionsExceptions的异常。 You have to handled this exceptions.您必须处理此异常。 Wrap your code with try and catch .用try和catch包装你的代码。
Example:-例子:-
try {
final result = await FirebaseFunctions.instance
.httpsCallable('deleteUser')
.call({});
} on FirebaseFunctionsException catch (error) {
print(error.message);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.