我从我的云函数(可调用HTTPS)抛出此错误:
throw new functions.https.HttpsError('not-found', 'User account not found')
然后,在Android上,我可以通过以下方式获取第一个参数(“未找到”)
functions.getHttpsCallable("myFunc").call(data).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if(e instanceof FirebaseFunctionsException) {
FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
FirebaseFunctionsException.Code code = ffe.getCode();
String errorName = code.name()
})
errorName是该参数。
但是在iOS上,错误代码没有“名称”。
functions.httpsCallable("myFunc").call(data) { (result, error) in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
let code = FunctionsErrorCode(rawValue: error.code)
let message = error.localizedDescription
let details = error.userInfo[FunctionsErrorDetailsKey]
}
}
}
我得到的只是一个int代码和第二个参数(“找不到用户帐户”)。错误名称在某处可用吗?我希望使用该名称来处理不同的错误。
答案 0 :(得分:2)
我找到了我想要的枚举:
Android:
@Override
public void onFailure(@NonNull Exception e) {
FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
FirebaseFunctionsException.Code code = ffe.getCode();
if(code.equals(FirebaseFunctionsException.Code.ALREADY_EXISTS)) {
// handle error
}
}
iOS:
functions.httpsCallable("myFunc").call(data) { (result, error) in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
let code = FunctionsErrorCode(rawValue: error.code)
if code == FunctionsErrorCode.alreadyExists {
// handle error
}
}
}
}