Skip to content

Commit

Permalink
docs(crashlytics): illustrate different methods for recording crashes (
Browse files Browse the repository at this point in the history
  • Loading branch information
russellwheatley committed Jul 13, 2023
1 parent 5063af5 commit d00c3ed
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
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) {
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

0 comments on commit d00c3ed

Please sign in to comment.