Please does anyone know how to catch firebase Auth exceptions on flutter and display them?
Note: I am not interested in the console (catcherror((e) print(e))
(21/02/20) EDIT: This answer is old and the other answers contains cross platform solutions so you should look at theirs first and treat this as a fallback solution.
The firebase auth plugin doesn't really have a proper cross-platform error code system yet so you have to handle errors for android and ios independently.
I'm currently using the temporary fix from this github issue: #20223
Do note since its a temp fix, don't expect it to be fully reliable as a permanent solution.
enum authProblems { UserNotFound, PasswordNotValid, NetworkError }
try {
FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: password,
);
} catch (e) {
authProblems errorType;
if (Platform.isAndroid) {
switch (e.message) {
case 'There is no user record corresponding to this identifier. The user may have been deleted.':
errorType = authProblems.UserNotFound;
break;
case 'The password is invalid or the user does not have a password.':
errorType = authProblems.PasswordNotValid;
break;
case 'A network error (such as timeout, interrupted connection or unreachable host) has occurred.':
errorType = authProblems.NetworkError;
break;
// ...
default:
print('Case ${e.message} is not yet implemented');
}
} else if (Platform.isIOS) {
switch (e.code) {
case 'Error 17011':
errorType = authProblems.UserNotFound;
break;
case 'Error 17009':
errorType = authProblems.PasswordNotValid;
break;
case 'Error 17020':
errorType = authProblems.NetworkError;
break;
// ...
default:
print('Case ${e.message} is not yet implemented');
}
}
print('The error is $errorType');
}
in Dart you can react to different Exceptions using the on
syntax. Since Firebase uses its own PlatformException you can easily catch them with:
try {
AuthResult result = await signUp(email, password);
} on PlatformException catch (e) {
print(e.message);
} on Exception catch (e) {
print(e);
}
PlatformException brings a code as well as a message which can be displayed in the UI, i.e. :
PlatformException(ERROR_EMAIL_ALREADY_IN_USE, The email address is already in use by another account., null)
I was stuck on this for a while too, i created this gist with all the available errors here with an example, covers all the platform exception codes
Example for handling sign up exceptions
Future<String> signUp(String email, String password, String firstName) async {
FirebaseUser user;
try {
AuthResult result = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
user = result.user;
name = user.displayName;
email = user.email;
Firestore.instance.collection('users').document(user.uid).setData({
"uid": user.uid,
"firstName": firstName,
"email": email,
"userImage": userImage,
});
} catch (error) {
switch (error.code) {
case "ERROR_OPERATION_NOT_ALLOWED":
errorMessage = "Anonymous accounts are not enabled";
break;
case "ERROR_WEAK_PASSWORD":
errorMessage = "Your password is too weak";
break;
case "ERROR_INVALID_EMAIL":
errorMessage = "Your email is invalid";
break;
case "ERROR_EMAIL_ALREADY_IN_USE":
errorMessage = "Email is already in use on different account";
break;
case "ERROR_INVALID_CREDENTIAL":
errorMessage = "Your email is invalid";
break;
default:
errorMessage = "An undefined Error happened.";
}
}
if (errorMessage != null) {
return Future.error(errorMessage);
}
return user.uid;
}
Example for handling sign in exceptions
Future<String> signIn(String email, String password) async {
FirebaseUser user;
try {
AuthResult result = await _auth.signInWithEmailAndPassword(
email: email, password: password);
user = result.user;
name = user.displayName;
email = user.email;
userId = user.uid;
} catch (error) {
switch (error.code) {
case "ERROR_INVALID_EMAIL":
errorMessage = "Your email address appears to be malformed.";
break;
case "ERROR_WRONG_PASSWORD":
errorMessage = "Your password is wrong.";
break;
case "ERROR_USER_NOT_FOUND":
errorMessage = "User with this email doesn't exist.";
break;
case "ERROR_USER_DISABLED":
errorMessage = "User with this email has been disabled.";
break;
case "ERROR_TOO_MANY_REQUESTS":
errorMessage = "Too many requests. Try again later.";
break;
case "ERROR_OPERATION_NOT_ALLOWED":
errorMessage = "Signing in with Email and Password is not enabled.";
break;
default:
errorMessage = "An undefined Error happened.";
}
}
if (errorMessage != null) {
return Future.error(errorMessage);
}
return user.uid;
}
I have also recently faced this error and, I have found out that the .catchError()
callback wasn't being called in the debug mode (which is when you click the Run->Start Debugging
button in VSCode).
However, when you type in flutter run -d , then, the .catchError()
method gets called back as it is not in debug mode.
To get your preferred simulator's code paste this line of code in the terminal:
instruments -s devices
If that doesn't work, you can also try pasting this:
xcrun simctl list
The ```.catchError()`` method will get called unlike before and the code inside that will get executed as expected!
Additionally, the app won't crash anymore with a PlatformException()
and instead you will get a log like this one:
[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'uid' was called on null.
Receiver: null
I have been facing this problem in Google Sign In too, in which the .catchError()
was not being called!
In conclusion, if you have some error with handling errors in Firebase Authentication, you should first try to first run in through the terminal. Thanks, and I hope this helps!
in Auth Class have this function:
Future signUpWithEmailAndPassword(String email, String password) async {
try {
AuthResult result = await _auth.createUserWithEmailAndPassword(
email: email,
password: password,
);
FirebaseUser user = result.user;
return user;
} catch (e) {
return e;
}
}
The catch error above returns a runTimeType of PlatformException and a PlatformException in flutter has 3 properties check here!
in your Dart file, implement this on button listeners:
String error = "";
dynamic result = await _auth.signUpWithEmailAndPassword(email, password);
if (result.runtimeType == PlatformException) {
if (result.message != null) {
setState(() {
error = result.message;
});
} else {
setState(() {
error = "Unknown Error";
});
}
}
Expanding on the accepted answer I thought it's worth to mention that:
firebase_auth
plugin has AuthException.rethrow
or better throw your own formatted exceptions and catch those at the UI level (where you'll know exactly the kind of error you'll get).try {
AuthResult authResult = await FirebaseAuth.instance.signInWithCredential(credential);
// Your auth logic ...
} on AuthException catch (e) {
print('''
caught firebase auth exception\n
${e.code}\n
${e.message}
''');
var message = 'Oops!'; // Default message
switch (e.code) {
case 'ERROR_WRONG_PASSWORD':
message = 'The password you entered is totally wrong!';
break;
// More custom messages ...
}
throw Exception(message); // Or extend this with a custom exception class
} catch (e) {
print('''
caught exception\n
$e
''');
rethrow;
}