Where is the documentation for FlutterFire FirebaseException codes? - firebase

I'm trying to handle Firebase errors in Flutter using FlutterFire. Various functions throw FirebaseException, for example if calling an HttpsCallable when there is no internet connection. I need to figure out what to do depending on the error - for example, if there is no internet connection, show a message, else if it's an unknown error log an exception.
The exception has a code to achieve this:
/// The optional code to accommodate the message.
///
/// Allows users to identify the exception from a short code-name, for example
/// "no-app" is used when a user attempts to read a [FirebaseApp] which does
/// not exist.
final String code;
But I can't find anywhere where these codes are documented, which kind of makes them useless. I've searched for ages. Am I missing something? Does anyone have a link? How can I achieve this?

Auth error code firebase in code
Core dart code
For user in firebase in code
and so on... Could not find one place where everything is added...

Related

Unity Firebase RTDB doesn't work after building

Unity version 2020.3.22f1,
Firebase SDK 9.0.0 dotnet4
I've imported both the analytics and real-time database SDK.
The Analytics works perfectly fine.
Regarding the database, building an Android app bundle and uploading to internal test or building an APK and uploading directly to my phone or building for IOS and uploading to test-flight all 3 results with an error.
This is how I initialize Firebase-
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
//init analytics
FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
DatabaseReference = FirebaseDatabase.DefaultInstance.RootReference;
Debug.Log(JsonConvert.SerializeObject(DatabaseReference.Database.App.Options));
});
This is how I use the database (increment the win field of a specific level) -
DatabaseReference.Child("Levels").Child($"Level{levelNum}").Child("Wins").RunTransaction((mutableData) =>
{
mutableData.Value = Int32.Parse(mutableData.Value.ToString()) + 1;
return TransactionResult.Success(mutableData);
});
Referring to the log above in the DatabaseRefrence initialization, in the editor, I can see all the configuration properties - databaseUrl,apikey,AppId, etc...
Debugging the APK on my phone the Options property only includes the databaseUrl.
And when trying to perform a transaction to the database an error is being thrown -
W/Unity: Exception in transaction delegate, aborting transaction
System.NullReferenceException: Object reference not set to an instance of an object.
at ....(Firebase.Database.MutableData mutableData)
Things I've tried so far-
I've added the SHA1/SHA256 of both my debug Keystore and googles console App integrity
I've checked and the XML files are being generated with all the details successfully at the streamingAssets folder and at Assets\Plugins\Android\FirebaseApp.androidlib\res\values\google-services.xml
I've tried Initializing the Firebase app manually as mentioned here - https://stackoverflow.com/a/66874818/7210967, doing that indeed results with the debug.log above to include all the Options parameters but the same error occurs as if it doesn't actually use it. (I've tried doing that both with the configuration files in place and removed them completely).
I've tried overriding the default app instance Options.
I've read some posts saying that Proguard obfuscation might cause errors with firebase? couldn't find anything related to Unity.
If anyone has any ideas, please share! ty
Transactions in Firebase Realtime Database work a bit differently than you might expect, as they immediately invoke you handler with the client's guess about the current value of the node, which in general is going to be null.
So when you call mutableData.Value in your code, you get back null and you then call ToString() on it, which leads to the error you get. To solve this, first check whether the mutableData.Value is null before invoking methods on it.
int current = mutableData.Value is null ? 0 : Int32.Parse(mutableData.Value.ToString());
mutableData.Value = current + 1;
return TransactionResult.Success(mutableData);
Syntax errors are possible in the above, as it's been a while since I wrote C#.
A transaction send both the SDKs guess and your new value based on that guess to the server, which then does a compare-and-set operation. If the guess doesn't match the actual value in the database, the server rejects the write with the current value, which the client then uses to call your transaction handler again with an updated current guess.

List of Firestore Error Codes for Flutter

When using Cloud Firestore in my Flutter application I want to use the error codes to return a localized error message to the user. However, I did not find a list of all the possible error codes that could occur. Is there a list like that or do I have to handle the errors differently?
You can see the list of possible exception codes at the gRPC or at the documentation. But, you need to change the code from uppercase to all lowercase and replace underscore with dash. For example:
PERMISSION_DENIED -> permission-denied
NOT_FOUND -> not-found
And so on.
Also, you need to catch FirebaseException instead of PlatformException in the new version of cloud_firestore.
Please have a look at the exception codes from the firebase firestore documentation.

How to properly call FirebaseFirestore.instance.clearPersistence() in Flutter?

I'm facing the same problem as this guy question
But his accepted answer didn't helped me.
The problem:
When an user signs out, and another different user signs in, all data shown on my app is from the previous signed out user due to firebase caching system. I searched about this issue and found a solution that consists in calling this method:
FirebaseFirestore.instance.clearPersistence();
But everytime and everywhere I place this line of code, throws an exception saying I cannot call this method when the client is running:
Exception has occurred.
PlatformException (PlatformException(failed-precondition, Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console., {code: failed-precondition, message: Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console., nativeErrorMessage: Persistence cannot be cleared while the client is running., nativeErrorCode: 9}))
so, how to call this method? or better, is there a best way to solve this problem?
It seems it's necessary to terminate the FirebaseFirestore.instance first.
At the end of my log off method I call:
await FirebaseFirestore.instance.terminate();
await FirebaseFirestore.instance.clearPersistence();
I get no errors thrown and everything seems to be working as it should now.
You should call it immediately after you initialize Firebase, and before you make the first query.
add this to your Login Button 'onPressed':
FirebaseFirestore.instance.terminate();
FirebaseFirestore.instance
.clearPersistence()
.then((value) => signinUser(email, password, context));

Flutter Crashlytics log caught exception

Looking for some clarification as to how one can log caught exceptions using flutter's firebase_crashlytics package.
If I understand correctly (and from running some sample code) Crashlytics.instance.log('text'); will only add logs to the next crash report, rather than send off a non-fatal issue itself.
I'm looking for functionality which is equivalent to Crashlytics.logException(e); on Android, e.g.
try {
throwException();
} catch (e) {
Crashlytics.logException(e);
}
which allows you to log caught exceptions so they appear as non-fatal issues in the Crashlytics dashboard.
Is this currently possible with flutter's firebase_crashlytics package?
Is calling Crashlytics.instance.recordError('text', StackTrace.current) the way to achieve this for now?
Many thanks!
Short answer, yes.
Crashlytics.instance.recordError() is the equivalent of Crashlytics.logException()
If you dig into the Flutter firebase_crashlytics source code, you can actually see what Android APIs are involved.
Flutter’s recordError() invokes the method Crashlytics#onError in the Android library.
And tracing Crashlytics#onError, you’ll see that it goes to Crashlytics.logException(exception);
Additional note, you’ll also notice why Crashlytics.instance.log() ”will only add logs to the next crash report”. These logs are added to a ListQueue<String> _logs which is then packaged into the next call of recordError()
A snippet of Flutter’s invocation of Crashlytics.logException():
_recordError(...) {
...
final String result = await channel
.invokeMethod<String>('Crashlytics#onError', <String, dynamic>{
'exception': "${exception.toString()}",
'context': '$context',
'information': _information,
'stackTraceElements': stackTraceElements,
'logs': _logs.toList(),
'keys': _prepareKeys(),
});
}
And some reference notes for Crashlytics.logException():
To reduce your users’ network traffic, Crashlytics batches logged
exceptions together and sends them the next time the app launches.
For any individual app session, only the most recent 8 logged
exceptions are stored.
To add to the accepted answer, Crashlytics.instance.recordError() has now been deprecated for the new method FirebaseCrashlytics.instance.recordError(exception, stack).
BONUS TIP:
I had this problem where all the logged exceptions are grouped under the same issue in Crashlytics dashboard. These might be different crashes of the same or different code components. Due to this, I had to manually go through each instance of the crash to verify.
From my own testing, I found out the grouping is based on the top-most line in the stack trace you passed into the method above. Luckily, Dart has an easy way to get the current stack trace using StackTrace.current.
So to properly group the issues: get the current stack trace at the time of the exception and pass it in FirebaseCrashlytics.instance.recordError(exception, stack).
Hope this helps someone out there, I looked everywhere on the internet for a similar issue but can't find any.

Flutter/Dart & Firebase Error Messages & Codes

I am implementing Firebase Auth into my Flutter app. How do I access error codes or user friendly error messages from Firebase? Is this available yet?
For example, on iOS you can get the localisedDescription from an error.
In Dart I have a onError: and catchError Futures which return an error object, but don't appear to have the relevant associated information.
it is available. the problem is that the catchError value is not an object, so you can't loop thru its keys. print/debugPrint just outputs the values without giving you any clue how to reference only one of them.
it has getter methods - code, message, and...details. 'details' is the human-readable result, but it's not documented on the firebase_auth flutter example. do this:
}).catchError((e) {
print(e.details); // code, message, details
});

Resources