How to catch a Firebase Auth specific exceptions

前端 未结 16 2310
慢半拍i
慢半拍i 2020-11-28 04:37

Using Firebase, how do I catch a specific exception and tell the user gracefully about it? E.g :

FirebaseAuthInvalidCredentialsException: The email ad

16条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 05:30

    You can throw the Exception returned by task.getException inside a try block and catch each type of Exception that may be thrown by the method you are using.

    Here is an example from the OnCompleteListener for the createUserWithEmailAndPassword method.

    if(!task.isSuccessful()) {
        try {
            throw task.getException();
        } catch(FirebaseAuthWeakPasswordException e) {
            mTxtPassword.setError(getString(R.string.error_weak_password));
            mTxtPassword.requestFocus();
        } catch(FirebaseAuthInvalidCredentialsException e) {
            mTxtEmail.setError(getString(R.string.error_invalid_email));
            mTxtEmail.requestFocus();
        } catch(FirebaseAuthUserCollisionException e) {
            mTxtEmail.setError(getString(R.string.error_user_exists));
            mTxtEmail.requestFocus();
        } catch(Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }
    

提交回复
热议问题