From 32eae500d39ed293561e163f914dfdd2477815b3 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Wed, 18 May 2022 17:07:53 +0530 Subject: [PATCH 1/5] doc(docs): Add sentry guide Signed-off-by: xsahil03x --- .../guides/error_reporting_with_sentry.mdx | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx diff --git a/docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx b/docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx new file mode 100644 index 00000000..4eb347d1 --- /dev/null +++ b/docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx @@ -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, they’re sure to crop up from time to time. Since buggy apps lead to unhappy users and customers, it’s 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: +``` + +### 3. Initialize the SDK to capture different unhandled errors automatically + +```dart +import 'package:sentry_flutter/sentry_flutter.dart'; + +Future 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 _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 [Sentry’s site](https://docs.sentry.io/platforms/flutter/). \ No newline at end of file From ef7f271a006392b4c7678f5218d39347aac89d66 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Wed, 18 May 2022 17:10:03 +0530 Subject: [PATCH 2/5] doc(repo): add complete example link Signed-off-by: xsahil03x --- docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx b/docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx index 4eb347d1..33d4d221 100644 --- a/docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx +++ b/docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx @@ -129,5 +129,8 @@ 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. +### Complete example +To view a working example, see the [Stream Sample app](https://github.com/GetStream/flutter-samples/tree/main/packages/stream_chat_v1). + ### Learn more Extensive documentation about using the Sentry SDK can be found on [Sentry’s site](https://docs.sentry.io/platforms/flutter/). \ No newline at end of file From 3c0969c904f0c3d8fa3439343bbc5d54174f2ee2 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Thu, 19 May 2022 11:48:49 +0200 Subject: [PATCH 3/5] fix action --- .github/workflows/dart_code_metrics.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/dart_code_metrics.yaml b/.github/workflows/dart_code_metrics.yaml index 7e3e41fd..dfbd0890 100644 --- a/.github/workflows/dart_code_metrics.yaml +++ b/.github/workflows/dart_code_metrics.yaml @@ -42,33 +42,33 @@ jobs: uses: dart-code-checker/dart-code-metrics-action@v2.0.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} - relative_path: 'packages/stream_chat' + relative_path: 'packages/melos_stream_chat' folders: ${{ env.folders }} - name: "Stream Chat Flutter Core Metrics" uses: dart-code-checker/dart-code-metrics-action@v2.0.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} - relative_path: 'packages/stream_chat_flutter_core' + relative_path: 'packages/melos_stream_chat_flutter_core' folders: ${{ env.folders }} - name: "Stream Chat Flutter Metrics" uses: dart-code-checker/dart-code-metrics-action@v2.0.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} - relative_path: 'packages/stream_chat_flutter' + relative_path: 'packages/melos_stream_chat_flutter' folders: ${{ env.folders }} - name: "Stream Chat Localizations Metrics" uses: dart-code-checker/dart-code-metrics-action@v2.0.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} - relative_path: 'packages/stream_chat_localizations' + relative_path: 'packages/melos_stream_chat_localizations' folders: ${{ env.folders }} - name: "Stream Chat Persistence Metrics" uses: dart-code-checker/dart-code-metrics-action@v2.0.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} - relative_path: 'packages/stream_chat_persistence' + relative_path: 'packages/melos_stream_chat_persistence' folders: ${{ env.folders }} From e7ba4bc8ca3841ef836fce47b2a8a4d2030aa46c Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Thu, 19 May 2022 11:52:00 +0200 Subject: [PATCH 4/5] fix action --- .github/workflows/dart_code_metrics.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/dart_code_metrics.yaml b/.github/workflows/dart_code_metrics.yaml index dfbd0890..7e3e41fd 100644 --- a/.github/workflows/dart_code_metrics.yaml +++ b/.github/workflows/dart_code_metrics.yaml @@ -42,33 +42,33 @@ jobs: uses: dart-code-checker/dart-code-metrics-action@v2.0.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} - relative_path: 'packages/melos_stream_chat' + relative_path: 'packages/stream_chat' folders: ${{ env.folders }} - name: "Stream Chat Flutter Core Metrics" uses: dart-code-checker/dart-code-metrics-action@v2.0.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} - relative_path: 'packages/melos_stream_chat_flutter_core' + relative_path: 'packages/stream_chat_flutter_core' folders: ${{ env.folders }} - name: "Stream Chat Flutter Metrics" uses: dart-code-checker/dart-code-metrics-action@v2.0.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} - relative_path: 'packages/melos_stream_chat_flutter' + relative_path: 'packages/stream_chat_flutter' folders: ${{ env.folders }} - name: "Stream Chat Localizations Metrics" uses: dart-code-checker/dart-code-metrics-action@v2.0.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} - relative_path: 'packages/melos_stream_chat_localizations' + relative_path: 'packages/stream_chat_localizations' folders: ${{ env.folders }} - name: "Stream Chat Persistence Metrics" uses: dart-code-checker/dart-code-metrics-action@v2.0.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} - relative_path: 'packages/melos_stream_chat_persistence' + relative_path: 'packages/stream_chat_persistence' folders: ${{ env.folders }} From 96914549903542defbedabe0a4b2de37cc8e8fb2 Mon Sep 17 00:00:00 2001 From: Gordon Hayes Date: Thu, 19 May 2022 12:53:33 +0200 Subject: [PATCH 5/5] docs: review and fixes --- .../guides/error_reporting_with_sentry.mdx | 58 +++++++++++-------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx b/docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx index 33d4d221..a192a613 100644 --- a/docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx +++ b/docusaurus/docs/Flutter/guides/error_reporting_with_sentry.mdx @@ -8,31 +8,36 @@ Error Reporting With Sentry ## Introduction -While one always tries to create apps that are free of bugs, they’re sure to crop up from time to time. Since buggy apps lead to unhappy users and customers, it’s 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. +While one always tries to create apps that are free of bugs, they're sure to crop up from time to time. Since buggy apps lead to unhappy users and customers, it's 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. -In this guide, learn how to report errors to the [Sentry](https://sentry.io/welcome/) crash reporting service using the following steps: +Whenever an error occurs, create a report containing the error that occurred and the associated stack trace. You can then send the report to an error tracking service, such as [Sentry](https://sentry.io/), [Rollbar](https://rollbar.com/), or [Firebase Crashlytics](https://firebase.google.com/docs/crashlytics). -### 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. +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 your users run into trouble. +In this guide, learn how to report Stream Chat 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 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. +- [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. + +Import the `sentry_flutter` package into your app. The sentry package makes it easier to send error reports to the Sentry error tracking service. ```yaml dependencies: sentry_flutter: ``` -### 3. Initialize the SDK to capture different unhandled errors automatically +### 3. Initialize the Sentry SDK + +Initialize the SDK to capture different unhandled errors automatically. ```dart import 'package:sentry_flutter/sentry_flutter.dart'; @@ -45,7 +50,7 @@ Future main() async { } ``` -Or, if you want to run your app in your own error zone runZonedGuarded: +Or, if you want to run your app in your own error zone, use `runZonedGuarded`: ```dart void main() async { @@ -55,7 +60,7 @@ void main() async { // In development mode, simply print to console. FlutterError.dumpErrorToConsole(details); } else { - // In production mode, report to the application zone to report to sentry. + // In production mode, report to the application zone to report to Sentry. Zone.current.handleUncaughtError(details.exception, details.stack!); } }; @@ -63,7 +68,7 @@ void main() async { Future _reportError(dynamic error, StackTrace stackTrace) async { // Print the exception to the console. if (kDebugMode) { - // Print the full stacktrace in debug mode. + // Print the full stack trace in debug mode. print(stackTrace); return; } else { @@ -84,13 +89,13 @@ void main() async { } ``` -Alternatively, you can pass the DSN to Flutter using the dart-define tag: +Alternatively, you can pass the DSN to Flutter using the **dart-define** tag: -```dart +```bash --dart-define SENTRY_DSN=https://example@sentry.io/example ``` -### 4. Integration with StreamChat applications +### 4. Integration With StreamChat Applications Override the default `logHandlerFunction` to send errors to Sentry. @@ -98,7 +103,7 @@ Override the default `logHandlerFunction` to send errors to Sentry. void sampleAppLogHandler(LogRecord record) async { if (kDebugMode) StreamChatClient.defaultLogHandler(record); - // report errors to sentry + // Report errors to Sentry if (record.error != null || record.stackTrace != null) { await Sentry.captureException( record.error, @@ -119,18 +124,21 @@ StreamChatClient buildStreamChatClient( } ``` -### 5. Capture errors programmatically +### 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: +you can use the API to manually 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. +For more information, see the [Sentry API](https://pub.dev/documentation/sentry_flutter/latest/sentry_flutter/sentry_flutter-library.html) docs on Pub. + +### Complete Example -### Complete example To view a working example, see the [Stream Sample app](https://github.com/GetStream/flutter-samples/tree/main/packages/stream_chat_v1). -### Learn more -Extensive documentation about using the Sentry SDK can be found on [Sentry’s site](https://docs.sentry.io/platforms/flutter/). \ No newline at end of file +### Learn More + +Extensive documentation about using the Sentry SDK can be found on [Sentry's site](https://docs.sentry.io/platforms/flutter/).