diff --git a/README.md b/README.md index bc43637..b5f3890 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ Workout Test and Diet 4 You Common Util Functions +Version 1.0.11 + Sentry and logging only in debugMode + Version 1.0.10 Firebase FCM for web diff --git a/lib/service/api.dart b/lib/service/api.dart index 77bdf71..a88a708 100644 --- a/lib/service/api.dart +++ b/lib/service/api.dart @@ -1,12 +1,12 @@ import 'dart:convert'; import 'dart:io'; import 'package:flutter/foundation.dart'; -import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:workouttest_util/service/webapi.dart'; import 'package:workouttest_util/util/logging.dart'; import 'package:workouttest_util/util/common.dart'; import 'package:workouttest_util/util/not_found_exception.dart'; import 'package:workouttest_util/model/cache.dart'; +import 'package:workouttest_util/util/track.dart'; class APIClient with Common, Logging { static final APIClient _singleton = APIClient._internal(); @@ -56,7 +56,7 @@ class APIClient with Common, Logging { } catch (exception) { log(exception.toString()); try { - await Sentry.captureException(exception); + Track().trackError(exception); } on Exception catch (e) { log(e.toString()); } @@ -103,7 +103,7 @@ class APIClient with Common, Logging { throw NotFoundException(message: "Not Found ${e.message}"); } on Exception catch (e) { log("Post Exception: $e"); - await Sentry.captureException(e); + Track().trackError(e); throw Exception("Network Error, please try again later"); } } @@ -144,7 +144,7 @@ class APIClient with Common, Logging { throw NotFoundException(message: "Not Found ${e.message}"); } on Exception catch (e) { log("Post Exception: $e"); - await Sentry.captureException(e); + Track().trackError(e); throw Exception("Network Error, please try again later"); } } diff --git a/lib/service/firebase_api.dart b/lib/service/firebase_api.dart index f9e23ab..1721704 100644 --- a/lib/service/firebase_api.dart +++ b/lib/service/firebase_api.dart @@ -4,7 +4,6 @@ import 'package:crypto/crypto.dart'; import 'package:flutter/foundation.dart'; import 'package:workouttest_util/model/cache.dart'; import 'package:workouttest_util/util/logging.dart' as logger; -import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:sign_in_with_apple/sign_in_with_apple.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_core/firebase_core.dart'; @@ -12,6 +11,7 @@ import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:firebase_remote_config/firebase_remote_config.dart'; import 'package:flutter_facebook_auth/flutter_facebook_auth.dart'; import 'package:google_sign_in/google_sign_in.dart'; +import 'package:workouttest_util/util/track.dart'; class FirebaseApi with logger.Logging { bool appleSignInAvailable = false; @@ -51,7 +51,7 @@ class FirebaseApi with logger.Logging { await Firebase.initializeApp(); } } on Exception catch(e) { - Sentry.captureException(e); + Track().trackError(e); log("Error in Firebase init: $e"); } @@ -69,7 +69,7 @@ class FirebaseApi with logger.Logging { }) .onError((err) { - Sentry.captureException(err); + Track().trackError(err); log("Error initializing Firebase Messaging for Web $err"); }); } else { @@ -94,7 +94,7 @@ class FirebaseApi with logger.Logging { } }); } catch (e) { - Sentry.captureException(e); + Track().trackError(e); log("Error initializing Firebase Messaging $e"); } } @@ -112,7 +112,7 @@ class FirebaseApi with logger.Logging { userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password); Cache().firebaseUid = userCredential.user!.uid; } on FirebaseAuthException catch (e) { - Sentry.captureException(e); + Track().trackError(e); if (e.code == 'user-not-found') { log('No user found for that email.'); rc = SIGN_IN_NOT_FOUND; @@ -132,7 +132,7 @@ class FirebaseApi with logger.Logging { userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email, password: password); Cache().firebaseUid = userCredential.user!.uid; } on FirebaseAuthException catch (e) { - Sentry.captureException(e); + Track().trackError(e); if (e.code == 'weak-password') { log('The password provided is too weak.'); rc = REGISTER_WEAK_PWD; @@ -144,7 +144,7 @@ class FirebaseApi with logger.Logging { } } catch (e) { log(e.toString()); - Sentry.captureException(e); + Track().trackError(e); throw Exception(e.toString()); } return rc; @@ -193,7 +193,7 @@ class FirebaseApi with logger.Logging { // not match the nonce in `appleCredential.identityToken`, sign in will fail. userCredential = await FirebaseAuth.instance.signInWithCredential(oauthCredential); } on FirebaseAuthException catch(e) { - Sentry.captureException(e); + Track().trackError(e); throw Exception(e); } Cache().firebaseUid = userCredential.user!.uid; @@ -231,7 +231,7 @@ class FirebaseApi with logger.Logging { // not match the nonce in `appleCredential.identityToken`, sign in will fail. userCredential = await FirebaseAuth.instance.signInWithCredential(oauthCredential); } on FirebaseAuthException catch(e) { - Sentry.captureException(e); + Track().trackError(e); throw Exception(e); } @@ -255,7 +255,7 @@ class FirebaseApi with logger.Logging { final GoogleSignInAccount? googleUser = await googleSignIn.signIn(); if (googleUser == null) { - Sentry.captureException(Exception("Google Sign In failed")); + Track().trackError(Exception("Google Sign In failed")); throw Exception("Google Sign In failed"); } @@ -292,8 +292,9 @@ class FirebaseApi with logger.Logging { final GoogleSignInAccount? googleUser = await googleSignIn.signIn(); if (googleUser == null) { - Sentry.captureException(Exception("Google Sign In failed")); - throw Exception("Google Sign In failed"); + String e = "Google Sign In failed"; + Track().trackError(e); + throw Exception(e); } // Obtain the auth details from the request @@ -329,7 +330,7 @@ class FirebaseApi with logger.Logging { Cache().firebaseUid = userData['id']; log(userData.toString()); } else { - Sentry.captureException(Exception(result.message)); + Track().trackError(Exception(result.message)); throw Exception("Facebook login was not successful"); } @@ -357,7 +358,7 @@ class FirebaseApi with logger.Logging { Cache().firebaseUid = userCredential.user!.uid; } else { - Sentry.captureException(Exception(result.message)); + Track().trackError(Exception(result.message)); throw Exception("Facebook login was not successful"); } diff --git a/lib/service/webapi.dart b/lib/service/webapi.dart index 4a755ae..443a3ee 100644 --- a/lib/service/webapi.dart +++ b/lib/service/webapi.dart @@ -1,10 +1,10 @@ -import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:workouttest_util/model/cache.dart'; import 'package:workouttest_util/util/common.dart'; import 'package:workouttest_util/util/logging.dart'; import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:workouttest_util/util/not_found_exception.dart'; +import 'package:workouttest_util/util/track.dart'; class APIWebClient with Common, Logging { static final APIWebClient _singleton = APIWebClient._internal(); @@ -46,7 +46,7 @@ class APIWebClient with Common, Logging { } on Exception catch (exception) { log(exception.toString()); try { - await Sentry.captureException(exception); + Track().trackError(exception); } on Exception catch (e) { log(e.toString()); } @@ -82,7 +82,7 @@ class APIWebClient with Common, Logging { throw NotFoundException(message: "Not Found ${e.message}"); } on Exception catch (e) { log("Post Exception: $e"); - await Sentry.captureException(e); + Track().trackError(e); throw Exception("Network Error, please try again later"); } } @@ -115,7 +115,7 @@ class APIWebClient with Common, Logging { throw NotFoundException(message: "Not Found ${e.message}"); } on Exception catch (e) { log("Post Exception: $e"); - await Sentry.captureException(e); + Track().trackError(e); throw Exception("Network Error, please try again later"); } } diff --git a/lib/util/logging.dart b/lib/util/logging.dart index 5ce9397..3d4cd4d 100644 --- a/lib/util/logging.dart +++ b/lib/util/logging.dart @@ -1,15 +1,16 @@ -import 'package:workouttest_util/util/env.dart'; +import 'package:flutter/foundation.dart'; import 'package:intl/intl.dart'; mixin Logging { void log(String message) { - DateTime time = DateTime.now(); - print(DateFormat('yyyy-MM-dd HH:mm:ss ').format(time) + message); + if ( kDebugMode ) { + DateTime time = DateTime.now(); + print(DateFormat('yyyy-MM-dd HH:mm:ss ').format(time) + message); + } } void trace(String message) { - String testEnv = EnvironmentConfig.test_env; - if (testEnv == "1") { + if ( kDebugMode ) { log(message); } } diff --git a/lib/util/track.dart b/lib/util/track.dart index f58b06b..11eb69a 100644 --- a/lib/util/track.dart +++ b/lib/util/track.dart @@ -1,3 +1,4 @@ +import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:workouttest_util/model/cache.dart'; import 'package:workouttest_util/util/logging.dart'; import 'package:workouttest_util/service/tracking_service.dart'; @@ -50,4 +51,10 @@ class Track with Logging { } } + + void trackError(Object e) { + if ( kReleaseMode ) { + Sentry.captureException(e); + } + } }