doc(docs): Add sentry guide

Signed-off-by: xsahil03x <xdsahil@gmail.com>
This commit is contained in:
Sahil Kumar
2022-05-18 17:07:53 +05:30
committed by xsahil03x
parent 8985a0fe69
commit 32eae500d3
@@ -0,0 +1,133 @@
---
id: error_reporting_with_sentry
sidebar_position: 15
title: Error Reporting With Sentry
---
Error Reporting With Sentry
## Introduction
While one always tries to create apps that are free of bugs, theyre sure to crop up from time to time. Since buggy apps lead to unhappy users and customers, its important to understand how often your users experience bugs and where those bugs occur. That way, you can prioritize the bugs with the highest impact and work to fix them.
How can you determine how often your users experiences bugs? Whenever an error occurs, create a report containing the error that occurred and the associated stacktrace. You can then send the report to an error tracking service, such as Sentry, Fabric, or Rollbar.
The error tracking service aggregates all of the crashes your users experience and groups them together. This allows you to know how often your app fails and where the users run into trouble.
In this guide, learn how to report errors to the [Sentry](https://sentry.io/welcome/) crash reporting service using the following steps:
### 1. Get a DSN from Sentry
Before reporting errors to Sentry, you need a “DSN” to uniquely identify your app with the Sentry.io service.
To get a DSN, use the following steps:
* [Create an account with Sentry](https://sentry.io/signup/).
* Log in to the account.
* Create a new Flutter project.
* Copy the code snippet that includes the DSN.
### 2. Import the Sentry package
Import the `sentry_flutter` package into the app. The sentry package makes it easier to send error reports to the Sentry error tracking service.
```yaml
dependencies:
sentry_flutter: <latest_version>
```
### 3. Initialize the SDK to capture different unhandled errors automatically
```dart
import 'package:sentry_flutter/sentry_flutter.dart';
Future<void> main() async {
await SentryFlutter.init(
(options) => options.dsn = 'https://example@sentry.io/example',
appRunner: () => runApp(const MyApp()),
);
}
```
Or, if you want to run your app in your own error zone runZonedGuarded:
```dart
void main() async {
/// Captures errors reported by the Flutter framework.
FlutterError.onError = (FlutterErrorDetails details) {
if (kDebugMode) {
// In development mode, simply print to console.
FlutterError.dumpErrorToConsole(details);
} else {
// In production mode, report to the application zone to report to sentry.
Zone.current.handleUncaughtError(details.exception, details.stack!);
}
};
Future<void> _reportError(dynamic error, StackTrace stackTrace) async {
// Print the exception to the console.
if (kDebugMode) {
// Print the full stacktrace in debug mode.
print(stackTrace);
return;
} else {
// Send the Exception and Stacktrace to sentry in Production mode.
await Sentry.captureException(error, stackTrace: stackTrace);
}
}
runZonedGuarded(
() async {
await SentryFlutter.init(
(options) => options.dsn = 'https://example@sentry.io/example',
);
runApp(const MyApp());
},
_reportError,
);
}
```
Alternatively, you can pass the DSN to Flutter using the dart-define tag:
```dart
--dart-define SENTRY_DSN=https://example@sentry.io/example
```
### 4. Integration with StreamChat applications
Override the default `logHandlerFunction` to send errors to Sentry.
```dart
void sampleAppLogHandler(LogRecord record) async {
if (kDebugMode) StreamChatClient.defaultLogHandler(record);
// report errors to sentry
if (record.error != null || record.stackTrace != null) {
await Sentry.captureException(
record.error,
stackTrace: record.stackTrace,
);
}
}
StreamChatClient buildStreamChatClient(
String apiKey, {
Level logLevel = Level.SEVERE,
}) {
return StreamChatClient(
apiKey,
logLevel: logLevel,
logHandlerFunction: sampleAppLogHandler, // Pass the overridden logHandlerFunction
);
}
```
### 5. Capture errors programmatically
Besides the automatic error reporting that Sentry generates by importing and initializing the SDK,
you can use the API to report errors to Sentry:
```dart
await Sentry.captureException(exception, stackTrace: stackTrace);
```
For more information, see the [Sentry API](https://pub.dev/documentation/sentry_flutter/latest/sentry_flutter/sentry_flutter-library.html) docs on pub.dev.
### Learn more
Extensive documentation about using the Sentry SDK can be found on [Sentrys site](https://docs.sentry.io/platforms/flutter/).