firebase-kotlin-sdk icon indicating copy to clipboard operation
firebase-kotlin-sdk copied to clipboard

FirebaseAuth exceptions are too generic

Open vladmircan opened this issue 1 year ago • 8 comments

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!

vladmircan avatar Dec 10 '23 19:12 vladmircan

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

nbransby avatar Dec 11 '23 01:12 nbransby

@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.

vladmircan avatar Dec 11 '23 11:12 vladmircan

@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?

nbransby avatar Apr 10 '24 05:04 nbransby

@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?

vladmircan avatar Apr 24 '24 18:04 vladmircan

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

nbransby avatar Apr 27 '24 04:04 nbransby

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())
}

mr-kew avatar May 13 '24 11:05 mr-kew

seeing the same thing on ios side

GeorgProhaska avatar Jul 15 '24 12:07 GeorgProhaska