From d3cd6012e6f5daf85d484f24b932835dc02b8a1e Mon Sep 17 00:00:00 2001 From: "Vicente J. Ruiz Jurado" Date: Tue, 14 Aug 2018 08:55:14 +0200 Subject: [PATCH] Better error handling in fetch data --- lib/mainCommon.dart | 11 ++--------- lib/models/firesApi.dart | 3 ++- lib/redux/fetchDataMiddleware.dart | 24 +++++++++++++----------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/mainCommon.dart b/lib/mainCommon.dart index 386cc66..fe5976d 100644 --- a/lib/mainCommon.dart +++ b/lib/mainCommon.dart @@ -35,13 +35,6 @@ void mainCommon(List> otherMiddleware) { injector.map((i) => store.state.serverUrl, key: "serverUrl"); injector.map((i) => store.state.gmapKey, key: "gmapKey"); -/* VoidCallback mainFn = () { - loadYourLocations().then((yl) { - // Run baby run! - - }); - }; */ - var useSentry = !globals.isDevelopment; SentryClient _sentry; @@ -73,7 +66,8 @@ void mainCommon(List> otherMiddleware) { }); } -Future _reportError(bool useSentry, SentryClient sentry, dynamic error, dynamic stackTrace) async { +Future _reportError(bool useSentry, SentryClient sentry, dynamic error, + dynamic stackTrace) async { // Print the exception to the console print('Caught error: $error'); if (!useSentry) { @@ -88,4 +82,3 @@ Future _reportError(bool useSentry, SentryClient sentry, dynamic error, dy ); } } - diff --git a/lib/models/firesApi.dart b/lib/models/firesApi.dart index d61014d..23df2bb 100644 --- a/lib/models/firesApi.dart +++ b/lib/models/firesApi.dart @@ -47,9 +47,10 @@ class FiresApi { final mobileToken = state.user.token; final String url = '${state .firesApiUrl}mobile/subscriptions/all/$apiKey/$mobileToken'; + // if (globals.isDevelopment) print('$url'); return await resty.get(url).go().then((response) { if (response.statusCode == 200) { - // print(response.body); + // if (globals.isDevelopment) print(response.body); final dataSubscriptions = json.decode(response.body)['data']['subscriptions']; List subscribed = []; diff --git a/lib/redux/fetchDataMiddleware.dart b/lib/redux/fetchDataMiddleware.dart index d0c8d0e..a939802 100644 --- a/lib/redux/fetchDataMiddleware.dart +++ b/lib/redux/fetchDataMiddleware.dart @@ -149,13 +149,13 @@ void fetchDataMiddleware(Store store, action, NextDispatcher next) { if (action is FetchYourLocationsAction) { // Use the api to fetch the YourLocations + loadYourLocations().then((localLocations) { api .fetchYourLocations(store.state) .then((List subscribedLocations) { // If it succeeds, dispatch a success action with the YourLocations. // Our reducer will then update the State using these YourLocations. // print('Subscribed to: ${subscribedLocations.length}'); - loadYourLocations().then((localLocations) { if (subscribedLocations is List) { // unsubscribe all locally to sync the subs state localLocations.forEach((location) => location.subscribed = false); @@ -167,30 +167,32 @@ void fetchDataMiddleware(Store store, action, NextDispatcher next) { }).subscribed = true; }); } + + store.dispatch(new FetchYourLocationsSucceededAction(localLocations)); + persistYourLocations(localLocations); + localLocations.forEach((yl) { api - .getFiresInLocation( - state: store.state, - lat: yl.lat, - lon: yl.lon, - distance: yl.distance) - .then((value) { + .getFiresInLocation( + state: store.state, + lat: yl.lat, + lon: yl.lon, + distance: yl.distance) + .then((value) { yl.currentNumFires = value.numFires; store.dispatch(new UpdateYourLocationAction(yl)); }); }); - store.dispatch(new FetchYourLocationsSucceededAction(localLocations)); - persistYourLocations(localLocations); Completer completer = action.refreshCallback; if (completer != null) { completer.complete(null); } }); - }).catchError((Exception error) { + }).catchError((onError) { // If it fails, dispatch a failure action. The reducer will // update the state with the error. - store.dispatch(new FetchYourLocationsFailedAction(error)); + store.dispatch(new FetchYourLocationsFailedAction(onError)); }); }