Skip to content

Commit 4069f3d

Browse files
filiphyjbanov
authored andcommitted
Add an example for FlutterError.onError and runZoned (#41)
1 parent 1915e5c commit 4069f3d

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

README.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,48 @@ main() async {
4747

4848
## Tips for catching errors
4949

50-
- use a `try/catch` block
51-
- create a `Zone` with an error handler, e.g. using [runZoned][run_zoned]
52-
- in Flutter, use [FlutterError.onError][flutter_error]
53-
- use `Isolate.current.addErrorListener` to capture uncaught errors in the root zone
50+
- Use a `try/catch` block, like in the example above.
51+
- Create a `Zone` with an error handler, e.g. using [runZoned][run_zoned].
52+
53+
```dart
54+
var sentry = SentryClient(dsn: "https://...");
55+
// Run the whole app in a zone to capture all uncaught errors.
56+
runZoned(
57+
() => runApp(MyApp()),
58+
onError: (Object error, StackTrace stackTrace) {
59+
try {
60+
sentry.captureException(
61+
exception: error,
62+
stackTrace: stackTrace,
63+
);
64+
print('Error sent to sentry.io: $error');
65+
} catch (e) {
66+
print('Sending report to sentry.io failed: $e');
67+
print('Original error: $error');
68+
}
69+
},
70+
);
71+
```
72+
- For Flutter-specific errors (such as layout failures), use [FlutterError.onError][flutter_error]. For example:
73+
74+
```dart
75+
var sentry = SentryClient(dsn: "https://...");
76+
FlutterError.onError = (details, {bool forceReport = false}) {
77+
try {
78+
sentry.captureException(
79+
exception: details.exception,
80+
stackTrace: details.stack,
81+
);
82+
} catch (e) {
83+
print('Sending report to sentry.io failed: $e');
84+
} finally {
85+
// Also use Flutter's pretty error logging to the device's console.
86+
FlutterError.dumpErrorToConsole(details, forceReport: forceReport);
87+
}
88+
};
89+
```
90+
- Use `Isolate.current.addErrorListener` to capture uncaught errors
91+
in the root zone.
5492

5593
[run_zoned]: https://api.dartlang.org/stable/dart-async/runZoned.html
5694
[flutter_error]: https://docs.flutter.io/flutter/foundation/FlutterError/onError.html

0 commit comments

Comments
 (0)