GitLiveApp / firebase-kotlin-sdk

A Kotlin-first SDK for Firebase
https://gitliveapp.github.io/firebase-kotlin-sdk/
Apache License 2.0
1.15k stars 156 forks source link

FirebaseAuth exceptions are too generic #442

Open vladmircan opened 11 months ago

vladmircan commented 11 months ago

First of all, a huge thank you for the work you're doing here! Now onto my issue:

I am in the process of migrating my app to Kotlin Multiplatform and I noticed some discrepancies in the way Firebase exceptions are thrown. Whereas the native SDK throws specific errors such as FirebaseAuthInvalidCredentialsException the gitlive variant throws a generic FirebaseException (with a descriptive message to be fair) thus making the error handling more difficult.

N.B. I am in the early stages of my transition so I don't know if the same applies to other Firebase libraries as well.

Please let me know if this is an inherent limitation or if steps could be taken to improve this. Thanks!

nbransby commented 11 months ago

It should throw the same exception, can you supply some more details on how to reproduce this?

vladmircan commented 10 months ago

@nbransby Hello, thanks for getting back to me! Sorry for not providing a code sample from the start but I first wanted to make sure it's not a known issue.

Anyways, I created this small app that showcases the problem I'm talking about: https://github.com/vladmircan/GitLiveFirebaseDemo

Let me know if there's anything else you need from me.

nbransby commented 6 months ago

@vladmircan I looked at your sample but it just looks like you are just choosing to catch the generic exception by using the FirebaseException base class instead of one of the subclasses such as FirebaseAuthInvalidCredentialsException?

vladmircan commented 6 months ago

@nbransby That is true, I am catching the error as generic because otherwise the app just crashes (the thrown error is the generic FirebaseException kind and not the specific FirebaseAuthInvalidCredentialsException).

The app basically just throws a FirebaseException with a message indicating that it is in fact a FirebaseAuthInvalidCredentialsException.

However, while re-testing my repo, I noticed something: If I also add the dependency to the native com.google.firebase:firebase-auth, it all of a sudden starts working correctly. Is this expected behaviour?

nbransby commented 6 months ago

No its not expected behavior - I think it would help if you share the full stack trace of the original FirebaseException

mr-kew commented 5 months ago

I have very similar issue. On android specific exception is thrown (like FirebaseAuthInvalidCredentialsException, etc) but on iOS it just returns FirebaseAuthException with NSError converted to string as message.

I think the bug is in the following piece of code from file auth.kt. It seems one of the whens always falls through to the else clause resulting in the generic exception.

I am on version 1.11.1 but the code does not seem to be changed in the latest versions.

private fun NSError.toException() = when(domain) {
    FIRAuthErrorDomain -> when(code) {
        FIRAuthErrorCodeInvalidActionCode,
        FIRAuthErrorCodeExpiredActionCode -> FirebaseAuthActionCodeException(toString())

        FIRAuthErrorCodeInvalidEmail -> FirebaseAuthEmailException(toString())

        FIRAuthErrorCodeCaptchaCheckFailed,
        FIRAuthErrorCodeInvalidPhoneNumber,
        FIRAuthErrorCodeMissingPhoneNumber,
        FIRAuthErrorCodeInvalidVerificationID,
        FIRAuthErrorCodeInvalidVerificationCode,
        FIRAuthErrorCodeMissingVerificationID,
        FIRAuthErrorCodeMissingVerificationCode,
        FIRAuthErrorCodeUserTokenExpired,
        FIRAuthErrorCodeInvalidCredential -> FirebaseAuthInvalidCredentialsException(toString())

        FIRAuthErrorCodeWeakPassword -> FirebaseAuthWeakPasswordException(toString())

        FIRAuthErrorCodeInvalidUserToken -> FirebaseAuthInvalidUserException(toString())

        FIRAuthErrorCodeRequiresRecentLogin -> FirebaseAuthRecentLoginRequiredException(toString())

        FIRAuthErrorCodeSecondFactorAlreadyEnrolled,
        FIRAuthErrorCodeSecondFactorRequired,
        FIRAuthErrorCodeMaximumSecondFactorCountExceeded,
        FIRAuthErrorCodeMultiFactorInfoNotFound -> FirebaseAuthMultiFactorException(toString())

        FIRAuthErrorCodeEmailAlreadyInUse,
        FIRAuthErrorCodeAccountExistsWithDifferentCredential,
        FIRAuthErrorCodeCredentialAlreadyInUse -> FirebaseAuthUserCollisionException(toString())

        FIRAuthErrorCodeWebContextAlreadyPresented,
        FIRAuthErrorCodeWebContextCancelled,
        FIRAuthErrorCodeWebInternalError -> FirebaseAuthWebException(toString())

        FIRAuthErrorCodeNetworkError -> FirebaseNetworkException(toString())

        else -> FirebaseAuthException(toString())
    }
    else -> FirebaseAuthException(toString())
}
GeorgProhaska commented 3 months ago

seeing the same thing on ios side

mr-kew commented 2 months ago

In 2.1.0 this issue still persists.

Also there seem to be two codes missing from the toException() method:

FIRAuthErrorCodeWrongPassword = 17009L
FIRAuthErrorCodeUserNotFound = 17011L