android - 如何在 kotlin 中使用协程处理 firebase auth 错误?
问题描述
我有一个挂起功能,可以使用 firebase auth 注册用户。
我知道有 auth 错误代码,我怎样才能在 try cath 中获取它们?
我正在考虑使用e.message
然后将它们翻译成我的语言,但我不知道这是否是一个好方法。
override suspend fun registerUserFirebase(email: String, password: String):Boolean {
return try {
auth.createUserWithEmailAndPassword(email, password).await()
true
} catch (e: Exception) {
if (e.message == "The email address is already in use by another account."){
// Do stuff
}
false
}
}
解决方案
关于协程,这里没有什么特别的。根据 API docs for createUserWithEmailAndPassword()可以产生不同类型的异常:
例外
- 如果密码不够强,则抛出 FirebaseAuthWeakPasswordException
- 如果电子邮件地址格式错误,则会引发 FirebaseAuthInvalidCredentialsException
- 如果已经存在具有给定电子邮件地址的帐户,则会引发 FirebaseAuthUserCollisionException
推荐阅读
- python - 为什么 pandas 有自己的日期时间对象 Timestamp?
- css - 如何在 flex 容器中显示 2 个项目并保持 flex 项目的设置宽度和高度?
- java - JPA中的b / w可为空关键字和可空关键字有什么区别?
- c - C编程语言练习1-18
- sql - 将字符串转换为时间的 SQL 函数
- python - Odoo 10 - 检索另一个模型的字段值
- amazon-web-services - AWS IoT 证书文件
- c - 程序进入无限循环而不是调用函数?
- express - 如何在 pug 中将字符串呈现为 javascript
- kubernetes - MongoDB 副本的 Operator 或 Helm 图表