From 6c47016c8bdcc788684ccc8c6b6ff8ea7b58c54c Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Fri, 17 Apr 2020 09:41:36 +0200 Subject: [PATCH] logException: Also log to Sentry --- lib/core/notes_cache.dart | 3 ++- lib/error_reporting.dart | 18 ++++++++++++++++++ lib/setup/autoconfigure.dart | 3 ++- lib/state_container.dart | 3 ++- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/core/notes_cache.dart b/lib/core/notes_cache.dart index 2b41ba28..62076e5f 100644 --- a/lib/core/notes_cache.dart +++ b/lib/core/notes_cache.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_crashlytics/flutter_crashlytics.dart'; +import 'package:gitjournal/error_reporting.dart'; import 'package:path/path.dart' as p; import 'package:collection/collection.dart'; @@ -110,7 +111,7 @@ class NotesCache { return json.decode(contents).cast(); } catch (ex, st) { Log.e("Exception - $ex for contents: $contents"); - await FlutterCrashlytics().logException(ex, st); + await logException(ex, st); return []; } } diff --git a/lib/error_reporting.dart b/lib/error_reporting.dart index d7eaa922..ca057b8d 100644 --- a/lib/error_reporting.dart +++ b/lib/error_reporting.dart @@ -107,3 +107,21 @@ Future reportError(Object error, StackTrace stackTrace) async { print("Uncaught Exception: $error"); print(stackTrace); } + +Future logException(Exception e, StackTrace stackTrace) async { + if (!reportCrashes) { + return; + } + + try { + final sentry = await getSentryClient(); + await sentry.captureException( + exception: e, + stackTrace: stackTrace, + ); + } catch (e) { + print("Failed to report with Sentry: $e"); + } + + return FlutterCrashlytics().logException(e, stackTrace); +} diff --git a/lib/setup/autoconfigure.dart b/lib/setup/autoconfigure.dart index e496800f..f465ed63 100644 --- a/lib/setup/autoconfigure.dart +++ b/lib/setup/autoconfigure.dart @@ -7,6 +7,7 @@ import 'package:flutter_crashlytics/flutter_crashlytics.dart'; import 'package:gitjournal/analytics.dart'; import 'package:gitjournal/apis/githost_factory.dart'; +import 'package:gitjournal/error_reporting.dart'; import 'package:gitjournal/settings.dart'; import 'package:gitjournal/utils/logger.dart'; @@ -122,7 +123,7 @@ class GitHostSetupAutoConfigureState extends State { }, ); - FlutterCrashlytics().logException(e, stacktrace); + logException(e, stacktrace); }); } diff --git a/lib/state_container.dart b/lib/state_container.dart index 589ff9dc..c1a41e68 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -11,6 +11,7 @@ import 'package:gitjournal/core/notes_cache.dart'; import 'package:gitjournal/core/notes_folder.dart'; import 'package:gitjournal/core/notes_folder_fs.dart'; import 'package:gitjournal/core/git_repo.dart'; +import 'package:gitjournal/error_reporting.dart'; import 'package:gitjournal/features.dart'; import 'package:gitjournal/settings.dart'; import 'package:gitjournal/utils/logger.dart'; @@ -117,7 +118,7 @@ class StateContainer with ChangeNotifier { appState.syncStatus = SyncStatus.Error; notifyListeners(); if (shouldLogGitException(e)) { - await FlutterCrashlytics().logException(e, stacktrace); + await logException(e, stacktrace); } if (!doNotThrow) rethrow; }