Log git exceptions in Crashlytics

This commit is contained in:
Vishesh Handa
2019-12-26 17:41:27 +01:00
parent cdd3f8d86c
commit 04af625a3b
2 changed files with 39 additions and 46 deletions

View File

@ -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;
}

View File

@ -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;
}