From 04af625a3b9613d13373e3913a56091b67eb956a Mon Sep 17 00:00:00 2001
From: Vishesh Handa <me@vhanda.in>
Date: Thu, 26 Dec 2019 17:41:27 +0100
Subject: [PATCH] Log git exceptions in Crashlytics

---
 lib/apis/git.dart      | 41 +--------------------------------------
 lib/core/git_repo.dart | 44 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 39 insertions(+), 46 deletions(-)

diff --git a/lib/apis/git.dart b/lib/apis/git.dart
index 4740d36d..c3f5457e 100644
--- a/lib/apis/git.dart
+++ b/lib/apis/git.dart
@@ -3,52 +3,13 @@ import 'dart:io';
 
 import 'package:flutter/services.dart';
 
-import 'package:flutter_crashlytics/flutter_crashlytics.dart';
-
 const _platform = MethodChannel('gitjournal.io/git');
 
-bool shouldIgnorePlatformException(PlatformException ex) {
-  var msg = ex.message.toLowerCase();
-  if (msg.contains("failed to resolve address for")) {
-    return true;
-  }
-  if (msg.contains("failed to connect to")) {
-    return true;
-  }
-  if (msg.contains("no address associated with hostname")) {
-    return true;
-  }
-  if (msg.contains("failed to connect to")) {
-    return true;
-  }
-  if (msg.contains("unauthorized")) {
-    return true;
-  }
-  if (msg.contains("invalid credentials")) {
-    return true;
-  }
-  if (msg.contains("failed to start ssh session")) {
-    return true;
-  }
-  return false;
-}
-
-Future invokePlatformMethod(String method, [dynamic arguments]) async {
-  try {
-    return await _platform.invokeMethod(method, arguments);
-  } on PlatformException catch (e, stacktrace) {
-    if (!shouldIgnorePlatformException(e)) {
-      await FlutterCrashlytics().logException(e, stacktrace);
-    }
-    throw e;
-  }
-}
-
 ///
 /// This gives us the directory where all the git repos will be stored
 ///
 Future<Directory> getGitBaseDirectory() async {
-  final String path = await invokePlatformMethod('getBaseDirectory');
+  final String path = await _platform.invokeMethod('getBaseDirectory');
   if (path == null) {
     return null;
   }
diff --git a/lib/core/git_repo.dart b/lib/core/git_repo.dart
index e6aa94b6..a5fbe757 100644
--- a/lib/core/git_repo.dart
+++ b/lib/core/git_repo.dart
@@ -1,8 +1,9 @@
 import 'dart:async';
 import 'dart:io';
 
-import 'package:fimber/fimber.dart';
 import 'package:flutter/foundation.dart';
+import 'package:flutter_crashlytics/flutter_crashlytics.dart';
+
 import 'package:git_bindings/git_bindings.dart';
 
 import 'package:gitjournal/core/note.dart';
@@ -104,17 +105,48 @@ class GitNoteRepository {
   Future<bool> sync() async {
     try {
       await _gitRepo.pull();
-    } on GitException catch (ex) {
-      Fimber.d(ex.toString());
+    } on GitException catch (e, stacktrace) {
+      if (shouldLogGitException(e)) {
+        await FlutterCrashlytics().logException(e, stacktrace);
+      }
+      throw e;
     }
 
     try {
       await _gitRepo.push();
-    } on GitException catch (ex) {
-      Fimber.d(ex.toString());
-      rethrow;
+    } on GitException catch (e, stacktrace) {
+      if (shouldLogGitException(e)) {
+        await FlutterCrashlytics().logException(e, stacktrace);
+      }
+      throw e;
     }
 
     return true;
   }
 }
+
+bool shouldLogGitException(GitException ex) {
+  var msg = ex.cause.toLowerCase();
+  if (msg.contains("failed to resolve address for")) {
+    return false;
+  }
+  if (msg.contains("failed to connect to")) {
+    return false;
+  }
+  if (msg.contains("no address associated with hostname")) {
+    return false;
+  }
+  if (msg.contains("failed to connect to")) {
+    return false;
+  }
+  if (msg.contains("unauthorized")) {
+    return false;
+  }
+  if (msg.contains("invalid credentials")) {
+    return false;
+  }
+  if (msg.contains("failed to start ssh session")) {
+    return false;
+  }
+  return true;
+}