1

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
  • The same issue is [discussed here](http://stackoverflow.com/q/37859582/4815718). As I see things, there is no simple and clean way to do what you want. In addition to the approaches presented in the answers here, you also can consider [this solution](http://stackoverflow.com/a/38490022/4815718). – Bob Snyder Sep 07 '16 at 18:21

2 Answers2

2

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()) {
        ...
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks a lot, but how do I get "FirebaseAuthUserCollisionException"? `case "FirebaseAuthUserCollisionException":`? – Ali Bdeir Sep 07 '16 at 17:06
  • A switch case cannot test classes in Java. If you want a switch case, you'll need to compare against `getErrorCode()`. If you want to compare against the class, you'll need to nest `if`s as @rafal says. – Frank van Puffelen Sep 07 '16 at 17:17
  • Fine, but how DO I compare the class? And if that's true, why did you include `switch(e.getErrorCode())` in your answer? Love your answers btw, very useful :) – Ali Bdeir Sep 07 '16 at 17:21
  • rafal's answer shows how to compare exception classes. If that doesn't work for you, show the code of how you did it (by editing your question). – Frank van Puffelen Sep 07 '16 at 17:36
  • Frank: The [documentation for FirebaseAuthUserCollisionException](https://developers.google.com/android/reference/com/google/firebase/auth/FirebaseAuthUserCollisionException) contains identifiers for error code string constants (e.g. `ERROR_EMAIL_ALREADY_IN_USE`), but these don't seem to be accessible as public members of some class. Am I not looking hard enough? An industrious developer found this [table of constants](http://stackoverflow.com/a/38038392/4815718) in a Firebase library. – Bob Snyder Sep 07 '16 at 18:01
  • @BobSnyder same here; it seems like Firebase library currently does not provide us public string constant error codes, so we may need to hardcode those string values.. which can be better. – shoheikawano Jul 04 '17 at 07:03
0

Try:

if(task.getException() instanceof FirebaseAuthUserCollisionException){
    //whatever
}
rafal
  • 3,120
  • 1
  • 17
  • 12