Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(crashlytics): illustrate different methods for recording crashes #11275

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 21 additions & 9 deletions docs/crashlytics/_customize-crash-reports.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,25 @@ to disk to be sent along with the next fatal report or when the app restarts.
You can automatically catch all "fatal" errors that are thrown within the Flutter
framework by overriding `FlutterError.onError` with
`FirebaseCrashlytics.instance.recordFlutterFatalError`. Alternatively,
to also catch "non-fatal" exceptions, override `FlutterError.onError` with `FirebaseCrashlytics.instance.recordFlutterError`:
to catch exceptions as "non-fatal" instead, override `FlutterError.onError` with `FirebaseCrashlytics.instance.recordFlutterError`:

```dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();

await Firebase.initializeApp();
bool weWantFatalErrorRecording = true;
FlutterError.onError = (errorDetails) {
if(weWantFatalErrorRecording){
FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
} else {
FirebaseCrashlytics.instance.recordFlutterError(errorDetails);
}
// If you want to record a "non-fatal" exception, use `FirebaseCrashlytics.instance.recordFlutterError` instead
const fatalError = true;
FlutterError.onError = (errorDetails) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this condition have any sense since it is const value?

if (fatalError) {
// If you want to record a "fatal" exception
FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
} else {
// If you want to record a "non-fatal" exception
FirebaseCrashlytics.instance.recordFlutterError(errorDetails);
}
};
};

runApp(MyApp());
Expand Down Expand Up @@ -67,8 +72,15 @@ Future<void> main() async {
FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
};
// Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
const fatalError = true;
PlatformDispatcher.instance.onError = (error, stack) {
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
if (fatalError) {
// If you want to record a "fatal" exception
FirebaseCrashlytics.instance.recordError(error, stack, fatal: fatalError);
} else {
// If you want to record a "non-fatal" exception
FirebaseCrashlytics.instance.recordError(error, stack);
}
return true;
};
runApp(MyApp());
Expand Down Expand Up @@ -118,7 +130,7 @@ await FirebaseCrashlytics.instance.recordError(
await FirebaseCrashlytics.instance.recordFlutterError(errorDetails);
```

You may also wish to log further information about the error which is possible
You may also want to log further information about the error which is possible
using the `information` property:

```dart
Expand Down
4 changes: 2 additions & 2 deletions docs/crashlytics/_force-test-crash.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

If you’ve added an error handler that calls
`FirebaseCrashlytics.instance.recordError(error, stack, fatal: true)` to the
top-level `Zone`, you can use the following code to add a button to your app
that, when pressed, throws a test exception:
`PlatformDispatcher.instance.onError`, you can use the following code to add a
button to your app that, when pressed, throws a test exception:

```dart
TextButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,28 @@ Future<void> main() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
const fatalError = true;
// Non-async exceptions
FlutterError.onError = (errorDetails) {
// If you wish to record a "non-fatal" exception, please use `FirebaseCrashlytics.instance.recordFlutterError` instead
FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
if (fatalError) {
// If you want to record a "fatal" exception
FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
// ignore: dead_code
} else {
// If you want to record a "non-fatal" exception
FirebaseCrashlytics.instance.recordFlutterError(errorDetails);
}
};
// Async exceptions
PlatformDispatcher.instance.onError = (error, stack) {
// If you wish to record a "non-fatal" exception, please remove the "fatal" parameter
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
if (fatalError) {
// If you want to record a "fatal" exception
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
// ignore: dead_code
} else {
// If you want to record a "non-fatal" exception
FirebaseCrashlytics.instance.recordError(error, stack);
}
return true;
};
runApp(MyApp());
Expand Down