From e6c3833765f0aa6f91425d0a5eb08ed08148d54e Mon Sep 17 00:00:00 2001 From: "Vicente J. Ruiz Jurado" Date: Fri, 17 Aug 2018 12:04:58 +0200 Subject: [PATCH] Sentry moved to lib. Catch markers errors --- lib/genericMap.dart | 91 ++++++++++++++++++++++++++++++---------- lib/mainCommon.dart | 28 +++---------- lib/models/appState.dart | 2 + lib/sentryReport.dart | 25 +++++++++++ 4 files changed, 103 insertions(+), 43 deletions(-) create mode 100644 lib/sentryReport.dart diff --git a/lib/genericMap.dart b/lib/genericMap.dart index 157a48e..1af4707 100644 --- a/lib/genericMap.dart +++ b/lib/genericMap.dart @@ -21,6 +21,7 @@ import 'layerSelectorMapPlugin.dart'; import 'models/appState.dart'; import 'models/fireMapState.dart'; import 'redux/actions.dart'; +import 'sentryReport.dart'; import 'slider.dart'; import 'zoomMapPlugin.dart'; @@ -37,6 +38,7 @@ class _ViewModel { final OnLocationEditCancel onEditCancel; final OnLocationEditing onEditing; final OnFalsePositive onFalsePositive; + final OnFirePressedInMap onFirePressed; _ViewModel( {@required this.mapState, @@ -49,6 +51,7 @@ class _ViewModel { @required this.onEditing, @required this.onEditConfirm, @required this.onFalsePositive, + @required this.onFirePressed, @required this.onEditCancel}); @override @@ -112,6 +115,9 @@ class _genericMapState extends State { }, onFalsePositive: (notif, type) => store .dispatch(new MarkFireAsFalsePositiveAction(notif, type)), + onFirePressed: (LatLng latLng, DateTime when) { + // _showFireDialog(latLng, when); + }, serverUrl: store.state.serverUrl, mapState: store.state.fireMapState); }, @@ -225,11 +231,15 @@ class _genericMapState extends State { : new DummyMapPluginOptions(), new MarkerLayerOptions( markers: buildMarkers( - mapState.status == FireMapStatus.viewFireNotification ? new LatLng(mapState.fireNotification.lat, mapState.fireNotification.lon): new LatLng(_location.lat, _location.lon), + mapState.status == FireMapStatus.viewFireNotification + ? new LatLng(mapState.fireNotification.lat, + mapState.fireNotification.lon) + : new LatLng(_location.lat, _location.lon), mapState.fires, mapState.industries, mapState.falsePos, - mapState.status == FireMapStatus.viewFireNotification), + mapState.status == FireMapStatus.viewFireNotification, + view.onFirePressed), ), // new AttributionPluginOptions(text: "© OpenStreetMap contributors"), new LayerSelectorMapPluginOptions(), @@ -380,34 +390,73 @@ class _genericMapState extends State { } } - List buildMarkers(LatLng pos, List fires, - List falsePosList, List industries, bool isNotif) { + List buildMarkers( + LatLng pos, + List fires, + List falsePosList, + List industries, + bool isNotif, + OnFirePressedInMap onFirePressed) { List markers = []; - print('building markers: fires: ${fires.length} falsePos: ${falsePosList.length} industries: ${industries.length}, isNotif: ${isNotif} '); + print('building markers: fires: ${fires.length} falsePos: ${falsePosList + .length} industries: ${industries.length}, isNotif: ${isNotif} '); const calibrate = false; // useful when we change the fire icons size falsePosList.forEach((falsePos) { - var coords = falsePos['geo']['coordinates']; - // print('false pos: ${coords}'); - var loc = LatLng(coords[1], coords[0]); - markers.add(FireMarker(loc, FireMarkType.falsePos)); - if (calibrate) markers.add(FireMarker(loc, FireMarkType.pixel)); + try { + var coords = falsePos['geo']['coordinates']; + // print('false pos: ${coords}'); + var loc = LatLng(coords[1], coords[0]); + markers.add(FireMarker(loc, FireMarkType.falsePos)); + if (calibrate) markers.add(FireMarker(loc, FireMarkType.pixel)); + } catch (e) { + print('Failed to process $falsePos'); + reportError(e, e.stackTrace); + } }); industries.forEach((industry) { - // print(fire['geo']['coordinates']); - var coords = industry['geo']['coordinates']; - var loc = LatLng(coords[1], coords[0]); - markers.add(FireMarker(loc, FireMarkType.industry)); - if (calibrate) markers.add(FireMarker(loc, FireMarkType.pixel)); + try { + // print(fire['geo']['coordinates']); + var coords = industry['geo']['coordinates']; + var loc = LatLng(coords[1], coords[0]); + markers.add(FireMarker(loc, FireMarkType.industry)); + if (calibrate) markers.add(FireMarker(loc, FireMarkType.pixel)); + } catch (e) { + print('Failed to process $industry'); + reportError(e, e.stackTrace); + } }); fires.forEach((fire) { - var loc = new LatLng(fire['lat'], fire['lon']); - markers.add( - FireMarker(loc, FireMarkType.fire, () => print('marker pressed'))); - markers.add(FireMarker(loc, FireMarkType.pixel)); + try { + var loc = new LatLng(fire['lat'], fire['lon']); + markers.add(FireMarker(loc, FireMarkType.fire, () { + onFirePressed(loc, DateTime.parse(fire['when'])); + print('fire $fire pressed'); + })); + markers.add(FireMarker(loc, FireMarkType.pixel)); + } catch (e) { + print('Failed to process $fire'); + reportError(e, e.stackTrace); + } }); - markers.add(FireMarker( - pos, isNotif ? FireMarkType.fire : FireMarkType.position)); + markers.add( + FireMarker(pos, isNotif ? FireMarkType.fire : FireMarkType.position)); if (calibrate) markers.add(FireMarker(pos, FireMarkType.pixel)); return markers; } + + void _showFireDialog(LatLng latLng, DateTime when) { + showDialog( + context: _scaffoldKey.currentContext, + builder: (_) => new AlertDialog( + content: new Text('FIXME'), + actions: [ + new FlatButton( + child: Text(S.of(_scaffoldKey.currentContext).CLOSE), + onPressed: () { + Navigator.pop(_scaffoldKey.currentContext); + }, + ), + ], + )); + } } diff --git a/lib/mainCommon.dart b/lib/mainCommon.dart index 3418eaf..436e351 100644 --- a/lib/mainCommon.dart +++ b/lib/mainCommon.dart @@ -13,6 +13,7 @@ import 'models/firesApi.dart'; import 'redux/fetchDataMiddleware.dart'; import 'redux/reducers.dart'; import 'package:package_info/package_info.dart'; +import 'sentryReport.dart'; Future loadPackageInfo() async { PackageInfo packageInfo = await PackageInfo.fromPlatform(); @@ -45,10 +46,10 @@ void mainCommon(List> otherMiddleware) { injector.map((i) => store.state.serverUrl, key: "serverUrl"); injector.map((i) => store.state.gmapKey, key: "gmapKey"); - var useSentry = !globals.isDevelopment; - - SentryClient _sentry; - if (useSentry) _sentry = SentryClient(dsn: secrets['sentryDSN']); + if (useSentry) { + SentryClient _sentry = SentryClient(dsn: secrets['sentryDSN']); + injector.map((i) => _sentry); + } // https://flutter.io/cookbook/maintenance/error-reporting/ runZoned>(() async { @@ -56,7 +57,7 @@ void mainCommon(List> otherMiddleware) { }, 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); + reportError(error, stackTrace); }); // Listen to store changes, and re-render when the state is updated @@ -76,20 +77,3 @@ void mainCommon(List> otherMiddleware) { }); }); } - -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, - ); - } -} diff --git a/lib/models/appState.dart b/lib/models/appState.dart index db930eb..b4cba15 100644 --- a/lib/models/appState.dart +++ b/lib/models/appState.dart @@ -13,6 +13,7 @@ import 'user.dart'; export 'fireMapState.dart'; import 'package:flutter_map/flutter_map.dart'; +import 'package:latlong/latlong.dart'; part 'appState.g.dart'; @@ -123,6 +124,7 @@ typedef void OnLocationEditing(YourLocation loc); typedef void OnLocationEditConfirm(YourLocation loc); typedef void OnLocationEditCancel(YourLocation loc); typedef void TapFireNotificationFunction(FireNotification notif); +typedef void OnFirePressedInMap(LatLng latLng, DateTime when); // typedef void OnReceivedFireNotificationFunction(FireNotification notif); typedef void DeleteFireNotificationFunction(FireNotification notif); typedef void DeleteAllFireNotificationFunction(); diff --git a/lib/sentryReport.dart b/lib/sentryReport.dart new file mode 100644 index 0000000..a50455e --- /dev/null +++ b/lib/sentryReport.dart @@ -0,0 +1,25 @@ +import 'dart:async'; + +import 'package:flutter_simple_dependency_injection/injector.dart'; +import 'package:sentry/sentry.dart'; + +import 'globals.dart' as globals; + +var useSentry = !globals.isDevelopment; + +Future reportError(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 + Injector.getInjector().get().captureException( + exception: error, + stackTrace: stackTrace, + ); + } +}