Ali Bdeir
Ali Bdeir

Reputation: 4365

How to get Firebase Exception into a switch statement

I currently have this code:

if (!task.isSuccessful() && task.getException() != null) {
   FirebaseCrash.report(task.getException());
   Log.w(TAG, "signInWithCredential", task.getException());
   Toast.makeText(SignUp.this, "Authentication failed: " + task.getException().getMessage(),Toast.LENGTH_LONG).show();
}

Sometimes a user gets a FirebaseAuthUserCollisionException, and I want to detect it in a switch statement like so:

switch(task.getException()){
    case /*I don't know what goes here*/:
       //Whatever
}

But as you read, I don't know what to put in the case statement. What do I put in there? In other words, where is the FirebaseAuthUserCollisionException located so I can access it? FirebaseAuthUserCollisionException is just a class so I can't currently do anything with it.

Upvotes: 1

Views: 650

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599131

Firebase Authentication exceptions all derive from FirebaseAuthException, which has a getErrorCode() method.

So:

if(task.getException() instanceof FirebaseAuthException){
    FirebaseAuthException e = (FirebaseAuthException)task.getException();
    switch (e.getErrorCode()) {
        ...
    }
}

Upvotes: 2

rafal
rafal

Reputation: 3260

Try:

if(task.getException() instanceof FirebaseAuthUserCollisionException){
    //whatever
}

Upvotes: 0

Related Questions