Emre Esen
Emre Esen

Reputation: 137

FirebaseNetworkException cannot be cast to com.google.firebase.auth.FirebaseAuthException

There is a cast problem which is FirebaseAuthException. In some cases, task exception cannot cast to FirebaseAuthException. How can i handle this situation? I want to try this casting process. If it will be success, i will show error with details to user. Otherwise If it will not success, i want to show just error.

   if(task.getException() != null){

    try {
        String errorCode = ((FirebaseAuthException) task.getException()).getErrorCode();

        switch(errorCode) {
            case "ERROR_EMAIL_ALREADY_IN_USE":
                Toast.makeText(LoginActivity.this, LoginActivity.this.getResources().getString(R.string.ERROR_EMAIL_ALREADY_IN_USE),
                        Toast.LENGTH_SHORT).show();
                stopIndicator();
                break;
            case "ERROR_INVALID_EMAIL":
                Toast.makeText(LoginActivity.this, LoginActivity.this.getResources().getString(R.string.ERROR_INVALID_EMAIL),
                        Toast.LENGTH_SHORT).show();
                stopIndicator();
                break;
            case "ERROR_WEAK_PASSWORD":
                Toast.makeText(LoginActivity.this, LoginActivity.this.getResources().getString(R.string.ERROR_WEAK_PASSWORD),
                        Toast.LENGTH_SHORT).show();
                stopIndicator();
                break;
            case "ERROR_CREDENTIAL_ALREADY_IN_USE":
                Toast.makeText(LoginActivity.this, "CREDENTIAL ALREADY IN USE",
                        Toast.LENGTH_SHORT).show();
                stopIndicator();
                break;
            default:
                Toast.makeText(LoginActivity.this, LoginActivity.this.getResources().getString(R.string.Error),
                        Toast.LENGTH_SHORT).show();
                stopIndicator();

        }
    }
    catch (WindowManager.BadTokenException e) {
        Toast.makeText(LoginActivity.this, LoginActivity.this.getResources().getString(R.string.Error),
                Toast.LENGTH_SHORT).show();
        stopIndicator();
    }

}else{
    Toast.makeText(LoginActivity.this, LoginActivity.this.getResources().getString(R.string.Error),
            Toast.LENGTH_SHORT).show();
    stopIndicator();
}

Upvotes: 2

Views: 1339

Answers (1)

Awscoolboy
Awscoolboy

Reputation: 36

String errorCode;
    if(task.getException()).getErrorCode() instanceof  FirebaseAuthException){
        errorCode = ((FirebaseAuthException) task.getException()).getErrorCode();

        switch(errorCode) {
            case "ERROR_EMAIL_ALREADY_IN_USE":
                Toast.makeText(LoginActivity.this, LoginActivity.this.getResources().getString(R.string.ERROR_EMAIL_ALREADY_IN_USE),
                        Toast.LENGTH_SHORT).show();
                stopIndicator();
                break;
            case "ERROR_INVALID_EMAIL":
                Toast.makeText(LoginActivity.this, LoginActivity.this.getResources().getString(R.string.ERROR_INVALID_EMAIL),
                        Toast.LENGTH_SHORT).show();
                stopIndicator();
                break;
            case "ERROR_WEAK_PASSWORD":
                Toast.makeText(LoginActivity.this, LoginActivity.this.getResources().getString(R.string.ERROR_WEAK_PASSWORD),
                        Toast.LENGTH_SHORT).show();
                stopIndicator();
                break;
            case "ERROR_CREDENTIAL_ALREADY_IN_USE":
                Toast.makeText(LoginActivity.this, "CREDENTIAL ALREADY IN USE",
                        Toast.LENGTH_SHORT).show();
                stopIndicator();
                break;
            default:
                Toast.makeText(LoginActivity.this, LoginActivity.this.getResources().getString(R.string.Error),
                        Toast.LENGTH_SHORT).show();
                stopIndicator();

        }


    }else{
         errorCode="";
    }

Upvotes: 1

Related Questions