Better error handling in fetch data

This commit is contained in:
Vicente J. Ruiz Jurado 2018-08-14 08:55:14 +02:00
parent 1a2cdfd814
commit d3cd6012e6
3 changed files with 17 additions and 21 deletions

View file

@ -35,13 +35,6 @@ void mainCommon(List<Middleware<AppState>> otherMiddleware) {
injector.map<String>((i) => store.state.serverUrl, key: "serverUrl"); injector.map<String>((i) => store.state.serverUrl, key: "serverUrl");
injector.map<String>((i) => store.state.gmapKey, key: "gmapKey"); injector.map<String>((i) => store.state.gmapKey, key: "gmapKey");
/* VoidCallback mainFn = () {
loadYourLocations().then((yl) {
// Run baby run!
});
}; */
var useSentry = !globals.isDevelopment; var useSentry = !globals.isDevelopment;
SentryClient _sentry; SentryClient _sentry;
@ -73,7 +66,8 @@ void mainCommon(List<Middleware<AppState>> otherMiddleware) {
}); });
} }
Future<Null> _reportError(bool useSentry, SentryClient sentry, dynamic error, dynamic stackTrace) async { Future<Null> _reportError(bool useSentry, SentryClient sentry, dynamic error,
dynamic stackTrace) async {
// Print the exception to the console // Print the exception to the console
print('Caught error: $error'); print('Caught error: $error');
if (!useSentry) { if (!useSentry) {
@ -88,4 +82,3 @@ Future<Null> _reportError(bool useSentry, SentryClient sentry, dynamic error, dy
); );
} }
} }

View file

@ -47,9 +47,10 @@ class FiresApi {
final mobileToken = state.user.token; final mobileToken = state.user.token;
final String url = '${state final String url = '${state
.firesApiUrl}mobile/subscriptions/all/$apiKey/$mobileToken'; .firesApiUrl}mobile/subscriptions/all/$apiKey/$mobileToken';
// if (globals.isDevelopment) print('$url');
return await resty.get(url).go().then((response) { return await resty.get(url).go().then((response) {
if (response.statusCode == 200) { if (response.statusCode == 200) {
// print(response.body); // if (globals.isDevelopment) print(response.body);
final dataSubscriptions = final dataSubscriptions =
json.decode(response.body)['data']['subscriptions']; json.decode(response.body)['data']['subscriptions'];
List<YourLocation> subscribed = []; List<YourLocation> subscribed = [];

View file

@ -149,13 +149,13 @@ void fetchDataMiddleware(Store<AppState> store, action, NextDispatcher next) {
if (action is FetchYourLocationsAction) { if (action is FetchYourLocationsAction) {
// Use the api to fetch the YourLocations // Use the api to fetch the YourLocations
loadYourLocations().then((localLocations) {
api api
.fetchYourLocations(store.state) .fetchYourLocations(store.state)
.then((List<YourLocation> subscribedLocations) { .then((List<YourLocation> subscribedLocations) {
// If it succeeds, dispatch a success action with the YourLocations. // If it succeeds, dispatch a success action with the YourLocations.
// Our reducer will then update the State using these YourLocations. // Our reducer will then update the State using these YourLocations.
// print('Subscribed to: ${subscribedLocations.length}'); // print('Subscribed to: ${subscribedLocations.length}');
loadYourLocations().then((localLocations) {
if (subscribedLocations is List) { if (subscribedLocations is List) {
// unsubscribe all locally to sync the subs state // unsubscribe all locally to sync the subs state
localLocations.forEach((location) => location.subscribed = false); localLocations.forEach((location) => location.subscribed = false);
@ -167,30 +167,32 @@ void fetchDataMiddleware(Store<AppState> store, action, NextDispatcher next) {
}).subscribed = true; }).subscribed = true;
}); });
} }
store.dispatch(new FetchYourLocationsSucceededAction(localLocations));
persistYourLocations(localLocations);
localLocations.forEach((yl) { localLocations.forEach((yl) {
api api
.getFiresInLocation( .getFiresInLocation(
state: store.state, state: store.state,
lat: yl.lat, lat: yl.lat,
lon: yl.lon, lon: yl.lon,
distance: yl.distance) distance: yl.distance)
.then((value) { .then((value) {
yl.currentNumFires = value.numFires; yl.currentNumFires = value.numFires;
store.dispatch(new UpdateYourLocationAction(yl)); store.dispatch(new UpdateYourLocationAction(yl));
}); });
}); });
store.dispatch(new FetchYourLocationsSucceededAction(localLocations));
persistYourLocations(localLocations);
Completer<Null> completer = action.refreshCallback; Completer<Null> completer = action.refreshCallback;
if (completer != null) { if (completer != null) {
completer.complete(null); completer.complete(null);
} }
}); });
}).catchError((Exception error) { }).catchError((onError) {
// If it fails, dispatch a failure action. The reducer will // If it fails, dispatch a failure action. The reducer will
// update the state with the error. // update the state with the error.
store.dispatch(new FetchYourLocationsFailedAction(error)); store.dispatch(new FetchYourLocationsFailedAction(onError));
}); });
} }