英文:
How to identify authentification error type in AWS Amplify using Kotlin?
问题
我想在用户通过Kotlin使用AWS Amplify登录时向他们显示不同的错误。这是我在Amplify.Auth.signIn()
的最后一个参数中设置的内容:
{ error ->
inputEmail.error = "检查电子邮件是否有效"
inputPassword.error = "检查密码是否有效"
})
“error”是一个“Throwable?”,我想将其转换为各种AWS异常并检查转换是否成功。然而,所有AWS Amplify异常都基于Java版本的“Throwable”。是否有办法使这些转换生效,或者是否有一种在Kotlin中识别错误类型的替代方法?
英文:
I want to display different errors to the user when they are logging in through AWS Amplify using Kotlin. This is what I have set up as my last parameter of Amplify.Auth.signIn()
:
{ error ->
inputEmail.error = "Check if the e-mail is valid"
inputPassword.error = "Check if the password is valid"
})
"error" is a "Throwable?" which I wanted to cast to various AWS exceptions and check whether the cast was a success. Yet all of the AWS Amplify exceptions are based on the Java version of "Throwable". Is there a way to make these casts work or is there an alternative way to identify error types in Kotlin?
答案1
得分: 2
以下是翻译好的部分:
"The last argument in 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 the input."
"在signIn(...)
方法中的最后一个参数是Consumer<AuthException>
类型的。这是一个接受AuthException
并执行某些操作的函数。因此,您不需要对输入进行向下转型。"
"There are a few types exception that extend AuthException
."
"有一些异常类型扩展了AuthException
。"
"As in this answer, I suggest is to exhaust those types using a when
construct. Paraphrasing:"
"就像这个答案中提到的,我建议使用when
结构详尽处理这些类型。重新表述如下:"
when (error) {
is SessionUnavailableOfflineException -> doSomething()
is InvalidAccountTypeException -> doSomethingElse()
// etc.
}
当 (error) {
是 SessionUnavailableOfflineException -> 做某事()
是 InvalidAccountTypeException -> 做其他事情()
// 等等。
}
"You can also check for errors in the active auth session with fetchAuthSession(...)
:"
"您还可以使用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()) }
)
Amplify.Auth.fetchAuthSession(
{ result ->
val cognitoAuthSession = result as AWSCognitoAuthSession
if (AuthSessionResult.Type.FAILURE == cognitoAuthSession.identityId.type) {
// 做某事
}
},
{ error -> Log.e("AuthQuickStart", error.toString()) }
)
英文:
The last argument in 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 the input.
There are a few types exception that extend AuthException
.
As in this answer, I suggest is to exhaust those types using a when
construct. Paraphrasing:
when (error) {
is SessionUnavailableOfflineException -> doSomething()
is InvalidAccountTypeException -> doSomethingElse()
// etc.
}
You can also check for errors in the active auth session with 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()) }
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论