如何使用 Kotlin 识别 AWS Amplify 中的身份验证错误类型?
How to identify authentification error type in AWS Amplify using Kotlin?
我想在用户使用 Kotlin 通过 AWS Amplify 登录时向他们显示不同的错误。这是我设置的 Amplify.Auth.signIn()
:
的最后一个参数
{ error ->
inputEmail.error = "Check if the e-mail is valid"
inputPassword.error = "Check if the password is valid"
})
“错误”是“Throwable?”我想将其转换为各种 AWS 异常并检查转换是否成功。然而,所有 AWS Amplify 异常都基于 Java 版本的“Throwable”。有没有办法让这些强制转换工作,或者有没有其他方法来识别 Kotlin 中的错误类型?
the signIn(...)
method is of type Consumer<AuthException>
. This is a function that accepts an AuthException
, and does something with it. So, you shouldn't need to downcast 输入中的最后一个参数。
有a few types exception that extend AuthException
.
与 一样,我建议使用 when
构造来穷尽这些类型。释义:
when (error) {
is SessionUnavailableOfflineException -> doSomething()
is InvalidAccountTypeException -> doSomethingElse()
// etc.
}
您还可以使用 fetchAuthSession(...)
:
检查活动身份验证会话中的错误
Amplify.Auth.fetchAuthSession(
{ result ->
val cognitoAuthSession = result as AWSCognitoAuthSession
if (AuthSessionResult.Type.FAILURE == cognitoAuthSession.identityId.type) {
// do stuff
}
},
{ error -> Log.e("AuthQuickStart", error.toString()) }
)
我想在用户使用 Kotlin 通过 AWS Amplify 登录时向他们显示不同的错误。这是我设置的 Amplify.Auth.signIn()
:
{ error ->
inputEmail.error = "Check if the e-mail is valid"
inputPassword.error = "Check if the password is valid"
})
“错误”是“Throwable?”我想将其转换为各种 AWS 异常并检查转换是否成功。然而,所有 AWS Amplify 异常都基于 Java 版本的“Throwable”。有没有办法让这些强制转换工作,或者有没有其他方法来识别 Kotlin 中的错误类型?
the signIn(...)
method is of type Consumer<AuthException>
. This is a function that accepts an AuthException
, and does something with it. So, you shouldn't need to downcast 输入中的最后一个参数。
有a few types exception that extend AuthException
.
与 when
构造来穷尽这些类型。释义:
when (error) {
is SessionUnavailableOfflineException -> doSomething()
is InvalidAccountTypeException -> doSomethingElse()
// etc.
}
您还可以使用 fetchAuthSession(...)
:
Amplify.Auth.fetchAuthSession(
{ result ->
val cognitoAuthSession = result as AWSCognitoAuthSession
if (AuthSessionResult.Type.FAILURE == cognitoAuthSession.identityId.type) {
// do stuff
}
},
{ error -> Log.e("AuthQuickStart", error.toString()) }
)