在可调用HTTPS的Firebase Cloud Functions中,iOS中是否提供错误代码的名称?

时间:2018-08-23 20:44:51

标签: ios swift firebase google-cloud-functions

我从我的云函数(可调用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代码和第二个参数(“找不到用户帐户”)。错误名称在某处可用吗?我希望使用该名称来处理不同的错误。

1 个答案:

答案 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
            }
        }
     }
 }