我试图构建一个firebase登录函数,但是即使抛出的异常被捕获,代码也会崩溃。
当我将异常作为日志输出时,代码可以正常工作,但是当我将其作为异常传递时,代码会崩溃。
我想将异常传递给MainActivity,以便在那里正确处理它,即将exception.message输出为吐司。
登录过程使用有效的登录数据,只有在firebase报告异常时才会崩溃。
internal var loginResult = MutableLiveData<LoginResult>()
x
inner class LoginResult {
var authResult: AuthResult? = null
var exception: Exception? = null
var isSuccessful = false
constructor(authResult: AuthResult?, isSuccessful: Boolean) {
this.authResult = authResult
this.isSuccessful = isSuccessful
}
constructor(authResult: AuthResult?, exception: Exception?, isSuccessful: Boolean) {
this.authResult = authResult
this.exception = exception
this.isSuccessful = isSuccessful
}
}
fun login(context: Context, email: String, password: String) {
FirebaseAuth.getInstance().signInWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
loginResult.value = LoginResult(task.result, task.isSuccessful)
startSession(context, LoginType.CREDENTIAL)
saveCredentialsEncrypted(context, email, password)
} else {
try {
loginResult.value = LoginResult(task.result, task.exception, task.isSuccessful)
} catch (e: FirebaseAuthInvalidCredentialsException) {
loginResult.value = LoginResult(task.result, e, task.isSuccessful)
} catch (e: Exception) {
loginResult.value = LoginResult(task.result, e, task.isSuccessful)
}
}
}
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.simplesaakotlin, PID: 23984
com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The password is invalid or the user does not have a password.
at com.google.android.gms.tasks.zzu.getResult(com.google.android.gms:play-services-tasks@@17.1.0:15)
at com.example.simplesaakotlin.auth.AuthManager$login$1.onComplete(AuthManager.kt:84)
at com.google.android.gms.tasks.zzj.run(com.google.android.gms:play-services-tasks@@17.1.0:4)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at com.google.android.gms.internal.tasks.zzb.dispatchMessage(com.google.android.gms:play-services-tasks@@17.1.0:6)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7660)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The password is invalid or the user does not have a password.
at com.google.android.gms.internal.firebase-auth-api.zztt.zza(com.google.firebase:firebase-auth@@20.0.2:28)
at com.google.android.gms.internal.firebase-auth-api.zzvb.zza(com.google.firebase:firebase-auth@@20.0.2:9)
at com.google.android.gms.internal.firebase-auth-api.zzvc.zzk(com.google.firebase:firebase-auth@@20.0.2:1)
at com.google.android.gms.internal.firebase-auth-api.zzuz.zzh(com.google.firebase:firebase-auth@@20.0.2:25)
at com.google.android.gms.internal.firebase-auth-api.zztq.zzk(com.google.firebase:firebase-auth@@20.0.2:1)
at com.google.android.gms.internal.firebase-auth-api.zzoi.zza(com.google.firebase:firebase-auth@@20.0.2:2)
at com.google.android.gms.internal.firebase-auth-api.zzvg.zza(com.google.firebase:firebase-auth@@20.0.2:25)
at com.google.android.gms.internal.firebase-auth-api.zzuq.zzf(com.google.firebase:firebase-auth@@20.0.2:4)
at com.google.android.gms.internal.firebase-auth-api.zzpy.zzj(com.google.firebase:firebase-auth@@20.0.2:5)
at com.google.android.gms.internal.firebase-auth-api.zztu.zzi(com.google.firebase:firebase-auth@@20.0.2:8)
at com.google.android.gms.internal.firebase-auth-api.zzsk.zzd(Unknown Source:15)
at com.google.android.gms.internal.firebase-auth-api.zzsj.accept(Unknown Source:6)
at com.google.android.gms.common.api.internal.zach.doExecute(com.google.android.gms:play-services-base@@17.3.0:2)
at com.google.android.gms.common.api.internal.zah.zaa(com.google.android.gms:play-services-base@@17.3.0:9)
at com.google.android.gms.common.api.internal.GoogleApiManager$zaa.zac(com.google.android.gms:play-services-base@@17.3.0:192)
at com.google.android.gms.common.api.internal.GoogleApiManager$zaa.zab(com.google.android.gms:play-services-base@@17.3.0:157)
at com.google.android.gms.common.api.internal.GoogleApiManager$zaa.zaa(com.google.android.gms:play-services-base@@17.3.0:125)
at com.google.android.gms.common.api.internal.GoogleApiManager.handleMessage(com.google.android.gms:play-services-base@@17.3.0:144)
at android.os.Handler.dispatchMessage(Handler.java:102)
at com.google.android.gms.internal.base.zap.dispatchMessage(com.google.android.gms:play-services-base@@17.3.0:8)
at android.os.Looper.loop(Looper.java:223)
at android.os.HandlerThread.run(HandlerThread.java:67)
的数据
UPDATE:抛出错误是因为我试图访问task.result时没有结果,请参阅try catch块。
要修复此错误,我将null作为authResult传递
fun login(context: Context, email: String, password: String) {
FirebaseAuth.getInstance().signInWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
loginResult.value = LoginResult(task.result, task.isSuccessful)
startSession(context, LoginType.CREDENTIAL)
saveCredentialsEncrypted(context, email, password)
} else {
loginResult.value = LoginResult(null, task.exception, task.isSuccessful)
}
}
}
型
1条答案
按热度按时间fnx2tebb1#
在我的例子中,FirebaseAuthException在释放模式下没有被抛出/捕获,但在释放模式下工作得很好。