有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用Kotlin识别AWS Amplify中的身份验证错误类型?

我想在用户使用Kotlin通过AWS Amplify登录时向他们显示不同的错误。这是我设置的放大的最后一个参数。啊。签名():

{ error ->
     inputEmail.error = "Check if the e-mail is valid"
     inputPassword.error = "Check if the password is valid"
})

“错误”是“可丢弃的”吗我想将其转换为各种AWS异常,并检查转换是否成功。然而,所有AWS Amplify异常都基于Java版本的“Throwable”。有没有一种方法可以让这些强制转换工作,或者有没有另一种方法可以识别Kotlin中的错误类型


共 (1) 个答案

  1. # 1 楼答案

    the ^{}方法中的最后一个参数的类型为Consumer<AuthException>。这是一个函数,它接受一个AuthException,并用它做一些事情。所以,你不需要downcast输入

    a few types exception that extend ^{}

    this answer一样,我建议使用when构造来耗尽这些类型。释义:

    when (error) {
        is SessionUnavailableOfflineException -> doSomething()
        is InvalidAccountTypeException -> doSomethingElse()
        // etc.
    }
    

    您还可以使用^{}检查活动身份验证会话中的错误:

    Amplify.Auth.fetchAuthSession(
        { result ->
            val cognitoAuthSession = result as AWSCognitoAuthSession
            if (AuthSessionResult.Type.FAILURE == cognitoAuthSession.identityId.type) {
                // do stuff
            }
        },
        { error -> Log.e("AuthQuickStart", error.toString()) }
    )