Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FirebaseAuth exceptions are too generic #442

Open
vladmircan opened this issue Dec 10, 2023 · 6 comments
Open

FirebaseAuth exceptions are too generic #442

vladmircan opened this issue Dec 10, 2023 · 6 comments

Comments

@vladmircan
Copy link

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
Copy link
Member

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

@vladmircan
Copy link
Author

vladmircan commented Dec 11, 2023

@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
Copy link
Member

@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
Copy link
Author

@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
Copy link
Member

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

@mr-kew
Copy link

mr-kew commented May 13, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants