Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add an example for FlutterError.onError and runZoned (getsentry#41)
  • Loading branch information
filiph authored and yjbanov committed Aug 30, 2019
commit 4069f3d0ec0bf561f5374229cc0b592651fcc232
46 changes: 42 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,48 @@ main() async {

## Tips for catching errors

- use a `try/catch` block
- create a `Zone` with an error handler, e.g. using [runZoned][run_zoned]
- in Flutter, use [FlutterError.onError][flutter_error]
- use `Isolate.current.addErrorListener` to capture uncaught errors in the root zone
- Use a `try/catch` block, like in the example above.
- Create a `Zone` with an error handler, e.g. using [runZoned][run_zoned].

```dart
var sentry = SentryClient(dsn: "https://...");
// Run the whole app in a zone to capture all uncaught errors.
runZoned(
() => runApp(MyApp()),
onError: (Object error, StackTrace stackTrace) {
try {
sentry.captureException(
exception: error,
stackTrace: stackTrace,
);
print('Error sent to sentry.io: $error');
} catch (e) {
print('Sending report to sentry.io failed: $e');
print('Original error: $error');
}
},
);
```
- For Flutter-specific errors (such as layout failures), use [FlutterError.onError][flutter_error]. For example:

```dart
var sentry = SentryClient(dsn: "https://...");
FlutterError.onError = (details, {bool forceReport = false}) {
try {
sentry.captureException(
exception: details.exception,
stackTrace: details.stack,
);
} catch (e) {
print('Sending report to sentry.io failed: $e');
} finally {
// Also use Flutter's pretty error logging to the device's console.
FlutterError.dumpErrorToConsole(details, forceReport: forceReport);
}
};
```
- Use `Isolate.current.addErrorListener` to capture uncaught errors
in the root zone.

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