diff --git a/lib/mainCommon.dart b/lib/mainCommon.dart index 2fca808..386cc66 100644 --- a/lib/mainCommon.dart +++ b/lib/mainCommon.dart @@ -1,7 +1,6 @@ import 'dart:async'; import 'package:comunes_flutter/comunes_flutter.dart'; -import 'package:fires_flutter/models/yourLocationPersist.dart'; import 'package:flutter/material.dart'; import 'package:flutter_simple_dependency_injection/injector.dart'; import 'package:redux/redux.dart'; @@ -26,10 +25,9 @@ void mainCommon(List> otherMiddleware) { initialState: new AppState( gmapKey: secrets['gmapKey'], firesApiKey: secrets['firesApiKey'], - serverUrl: secrets['firesApiUrl'], + serverUrl: secrets['firesApiUrl'], firesApiUrl: secrets['firesApiUrl'] + "api/v1/"), - middleware: List.from(otherMiddleware) - ..add(fetchDataMiddleware)); + middleware: List.from(otherMiddleware)..add(fetchDataMiddleware)); injector.map>((i) => store); injector.map((i) => store.state.firesApiUrl, key: "firesApiUrl"); @@ -37,33 +35,57 @@ void mainCommon(List> otherMiddleware) { injector.map((i) => store.state.serverUrl, key: "serverUrl"); injector.map((i) => store.state.gmapKey, key: "gmapKey"); - VoidCallback mainFn = () { +/* VoidCallback mainFn = () { loadYourLocations().then((yl) { // Run baby run! - runApp(new FiresApp(store)); - }); - }; - if (!globals.isDevelopment) - // Run in production with sentry catch - main(mainFn, secrets['sentryDSN']); - else - mainFn(); + }); + }; */ + + var useSentry = !globals.isDevelopment; + + SentryClient _sentry; + if (useSentry) _sentry = SentryClient(dsn: secrets['sentryDSN']); + + // https://flutter.io/cookbook/maintenance/error-reporting/ + runZoned>(() async { + runApp(new FiresApp(store)); + }, onError: (error, stackTrace) { + // Whenever an error occurs, call the `_reportError` function. This will send + // Dart errors to our dev console or Sentry depending on the environment. + _reportError(useSentry, _sentry, error, stackTrace); + }); // Listen to store changes, and re-render when the state is updated store.onChange.listen((state) { // print('Store onChange'); }); + FlutterError.onError = (FlutterErrorDetails details) { + if (!useSentry) { + // 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); + } + }; }); } -main(mainFn, key) async { - SentryClient sentry = new SentryClient(dsn: key); - mainFn(); - try {} catch (error, stackTrace) { - await sentry.captureException( +Future _reportError(bool useSentry, SentryClient sentry, dynamic error, dynamic stackTrace) async { + // Print the exception to the console + print('Caught error: $error'); + if (!useSentry) { + // Print the full stacktrace in debug mode + print(stackTrace); + return; + } else { + // Send the Exception and Stacktrace to Sentry in Production mode + sentry.captureException( exception: error, stackTrace: stackTrace, ); } } +