Skip to content

Commit

Permalink
Unify the handling of error messages
Browse files Browse the repository at this point in the history
- Define a new function (displayError) that deals with undefined messages,
  formatting the error stack, etc.
- Replace all .catch(function(error)) calls with a call to displayError so that
  we can:
  - have a unified format that we can modify in one location
  - display meaningful error messages that should make it easier to figure out
    what is wrong.

This makes the error message from e-mission/e-mission-docs#378
more meaningful

Bonus fix: fix the formatting in e-mission@94ffa9f I am not sure how this ever worked!
  • Loading branch information
shankari committed May 8, 2019
1 parent 8b9a758 commit c5f5380
Show file tree
Hide file tree
Showing 22 changed files with 66 additions and 105 deletions.
4 changes: 3 additions & 1 deletion www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ angular.module('emission', ['ionic',
Logger.log("connectionConfigString = "+JSON.stringify(connectionConfig.data));
window.cordova.plugins.BEMConnectionSettings.setSettings(connectionConfig.data);
}).catch(function(err) {
// not displaying the error here since we have a backup
Logger.log("error "+JSON.stringify(err)+" while reading connection config, reverting to defaults");
window.cordova.plugins.BEMConnectionSettings.getDefaultSettings().then(function(defaultConfig) {
Logger.log("defaultConfig = "+JSON.stringify(defaultConfig));
window.cordova.plugins.BEMConnectionSettings.setSettings(defaultConfig);
}).catch(function(err) {
Logger.log("error "+JSON.stringify(err)+" reading or setting defaults, giving up");
// displaying the error here since we don't have a backup
Logger.displayError("Error reading or setting connection defaults", err);
});
});
});
Expand Down
5 changes: 3 additions & 2 deletions www/js/common/services.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

angular.module('emission.main.common.services', [])
angular.module('emission.main.common.services', ['emission.plugin.logger'])

.factory('CommonGraph', function($rootScope, $http) {
.factory('CommonGraph', function($rootScope, $http, Logger) {
var commonGraph = {};
commonGraph.data = {};
commonGraph.UPDATE_DONE = "COMMON_GRAPH_UPDATE_DONE";
Expand All @@ -25,6 +25,7 @@ angular.module('emission.main.common.services', [])
cmGraph = commonGraph.createEmpty();
}
} catch(err) {
// No popup for this since it is the common case and we have a fallback
window.Logger.log("Error "+err+" while parsing common trip data");
// If there was an error in parsing the current common trips, and
// there is no existing cached common trips, we create a blank
Expand Down
5 changes: 2 additions & 3 deletions www/js/control/general-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ angular.module('emission.main.control',['emission.services',
$ionicPopup.alert({template: 'all data pushed!'});
}
}).catch(function(error) {
$ionicPopup.alert({template: 'error -> '+JSON.stringify(error)});
Logger.displayError("Error while forcing sync", error);
});
};

Expand Down Expand Up @@ -491,8 +491,7 @@ angular.module('emission.main.control',['emission.services',
handleConsent(resultDoc);
}
}, function(error) {
$ionicPopup.alert({title: "Error reading consent document from cache",
template: error});
Logger.displayError("Error reading consent document from cache", error)
});
}

Expand Down
5 changes: 2 additions & 3 deletions www/js/diary/current.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@
});
});
}).catch(function(error) {
Logger.log("While loading location data, error "+JSON.stringify(error));
$ionicPopup.alert({"template": "While loading location data, error = "+ JSON.stringify(error)})
Logger.displayError("Error while loading location data", error);
});
console.log($scope.mapCtrl);
};
Expand Down Expand Up @@ -266,7 +265,7 @@
PostTripManualMarker.showSheet($scope.features, latlng, ts, marker, _map);
})
.catch(function(error) {
Logger.log("error while getting map current from leafletData");
Logger.displayError("Error while getting selected map location ", error);
});
};

Expand Down
2 changes: 1 addition & 1 deletion www/js/diary/detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ angular.module('emission.main.diary.detail',['ui-leaflet', 'ng-walkthrough',
nzTour.start(tour).then(function(result) {
Logger.log("detail walkthrough start completed, no error");
}).catch(function(err) {
Logger.log("detail walkthrough start errored" + err);
Logger.displayError("detail walkthrough start errored", err);
});
};

Expand Down
2 changes: 1 addition & 1 deletion www/js/diary/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ angular.module('emission.main.diary.list',['ui-leaflet',
nzTour.start(tour).then(function(result) {
Logger.log("list walkthrough start completed, no error");
}).catch(function(err) {
Logger.log("list walkthrough start errored" + err);
Logger.displayError("list walkthrough start errored", err);
});
};

Expand Down
2 changes: 1 addition & 1 deletion www/js/diary/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,9 +970,9 @@ angular.module('emission.main.diary.services', ['emission.plugin.logger',
}).then(function(combinedTripList) {
processOrDisplayNone(day, combinedTripList);
}).catch(function(error) {
Logger.log("while reading data from cache for "+day +" error = "+JSON.stringify(error));
console.log("About to hide loading overlay");
$ionicLoading.hide();
Logger.displayError("while reading data from cache for "+day, error);
})
});
}
Expand Down
3 changes: 2 additions & 1 deletion www/js/goals.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ angular.module('emission.main.goals',['emission.services', 'emission.plugin.logg
.then(function(event) {
// success
})
.catch(function(event) {
.catch(function(error) {
Logger.displayError("Error while launching habitica website", error);
// error
});

Expand Down
8 changes: 3 additions & 5 deletions www/js/heatmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ angular.module('emission.main.heatmap',['ui-leaflet', 'emission.services',
}
$scope.countData.isLoading = false;
}, function(error) {
Logger.log("Got error %s while trying to read heatmap data" +
JSON.stringify(error));
Logger.displayError("Error while trying to read heatmap data", error);
$scope.countData.isLoading = false;
});
};
Expand Down Expand Up @@ -297,8 +296,7 @@ angular.module('emission.main.heatmap',['ui-leaflet', 'emission.services',
}
$scope.stressData.isLoading = false;
}, function(error) {
Logger.log("Got error %s while trying to read heatmap data" +
JSON.stringify(error));
Logger.displayError("Error while trying to read stress data", error);
$scope.stressData.isLoading = false;
});
};
Expand Down Expand Up @@ -361,7 +359,7 @@ angular.module('emission.main.heatmap',['ui-leaflet', 'emission.services',
nzTour.start(tour).then(function(result) {
Logger.log("heatmap walkthrough start completed, no error");
}).catch(function(err) {
Logger.log("heatmap walkthrough start errored" + err);
Logger.displayError("Error in heatmap walkthrough", err);
});
};

Expand Down
9 changes: 2 additions & 7 deletions www/js/incident/post-trip-map-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,8 @@ angular.module('emission.incident.posttrip.map',['ui-leaflet', 'ng-walkthrough',
$ionicLoading.hide();
})
.catch(function(error) {
var errStr = JSON.stringify(error);
$ionicLoading.hide();
Logger.log(errStr);
$ionicPopup.alert({
title: "Unable to retrieve data",
template: errStr
});
Logger.displayError("Unable to retrieve location data for map", error);
});
}

Expand Down Expand Up @@ -176,7 +171,7 @@ angular.module('emission.incident.posttrip.map',['ui-leaflet', 'ng-walkthrough',
nzTour.start(tour).then(function(result) {
Logger.log("list walkthrough start completed, no error");
}).catch(function(err) {
Logger.log("incident walkthrough start errored" + err);
Logger.displayError("incident walkthrough start errored", err);
});
};

Expand Down
15 changes: 5 additions & 10 deletions www/js/incident/post-trip-prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,8 @@ angular.module('emission.incident.posttrip.prompt', ['emission.plugin.logger'])
Logger.log("Finished registering "+notifyPlugin.TRIP_END+" with result "+JSON.stringify(result));
})
.catch(function(error) {
Logger.log(JSON.stringify(error));
$ionicPopup.alert({
title: "Unable to register notifications for trip end",
template: JSON.stringify(error)
});
});;
Logger.displayError("Unable to register notifications for trip end", error);
});
}

var getFormattedTime = function(ts_in_secs) {
Expand Down Expand Up @@ -209,10 +205,9 @@ angular.module('emission.incident.posttrip.prompt', ['emission.plugin.logger'])
});
}
}).catch(function(error) {
$ionicPopup.alert({
title: "Error while muting notifications for trip end. Try again later.",
template: JSON.stringify(error)
});
Logger.displayError(
"Error while muting notifications for trip end. Try again later.",
error);
});
}
});
Expand Down
15 changes: 4 additions & 11 deletions www/js/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,7 @@ angular.module('emission.main.metrics',['nvd3', 'emission.services', 'ionic-date
})
.catch(function(error) {
$ionicLoading.hide();
$ionicPopup.alert({
title: "Error Loading Data",
template: JSON.stringify(error)
});
console.log(error);
Logger.displayError("Error loading user data", error);
})

getAggMetricsFromServer().then(function(results) {
Expand All @@ -518,11 +514,8 @@ angular.module('emission.main.metrics',['nvd3', 'emission.services', 'ionic-date
$ionicLoading.hide();
$scope.carbonData.aggrCarbon = "Unknown";
$scope.caloriesData.aggrCalories = "Unknown...";
$ionicPopup.alert({
title: "Error loading aggregate data, averages not available",
template: JSON.stringify(error)
});
console.log(error);
Logger.displayError("Error loading aggregate data, averages not available",
error);
});
};

Expand Down Expand Up @@ -1159,7 +1152,7 @@ angular.module('emission.main.metrics',['nvd3', 'emission.services', 'ionic-date
showTodayButton: true,
dateFormat: 'MMM dd yyyy',
closeOnSelect: false,
# add this instruction if you want to exclude a particular weekday, e.g. Saturday disableWeekdays: [6]
// add this instruction if you want to exclude a particular weekday, e.g. Saturday disableWeekdays: [6]
};

$scope.pickFromDay = function() {
Expand Down
11 changes: 10 additions & 1 deletion www/js/plugin/logger.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
angular.module('emission.plugin.logger', [])

.factory('Logger', function($window, $state, $interval, $rootScope) {
.factory('Logger', function($window, $ionicPopup) {
var loggerJs = {}
loggerJs.log = function(message) {
$window.Logger.log($window.Logger.LEVEL_DEBUG, message);
}
loggerJs.displayError = function(title, error) {
var display_msg = error.message + "\n" + error.stack;
if (!angular.isDefined(error.message)) {
display_msg = JSON.stringify(error);
}
$ionicPopup.alert({"title": title, "template": display_msg});
console.log(title + display_msg);
$window.Logger.log($window.Logger.LEVEL_ERROR, title + display_msg);
}
return loggerJs;
});
4 changes: 1 addition & 3 deletions www/js/recent.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ angular.module('emission.main.recent', ['ngCordova', 'emission.services'])
// This should really be within a try/catch/finally block
$scope.$broadcast('scroll.refreshComplete');
}, function(error) {
$ionicPopup.alert({title: "Error updating entries",
template: JSON.stringify(error)})
.then(function(res) {console.log("finished showing alert");});
window.logger.Logger.displayError("Error updating entries", error);
})
}

Expand Down
11 changes: 4 additions & 7 deletions www/js/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ angular.module('emission.services', ['emission.plugin.logger'])
})
.service('ControlHelper', function($cordovaEmailComposer,
$ionicPopup,
CommHelper) {
CommHelper,
Logger) {
this.emailLog = function() {
var parentDir = "unknown";

Expand Down Expand Up @@ -391,14 +392,10 @@ angular.module('emission.services', ['emission.plugin.logger'])
.then(writeDumpFile)
.then(emailData)
.then(function() {
window.Logger.log(window.Logger.LEVEL_DEBUG,
"Email queued successfully");
Logger.log("Email queued successfully");
})
.catch(function(error) {
window.Logger.log(window.Logger.LEVEL_INFO,
"Email cancel reported, seems to be an error on android");
$ionicPopup.alert({'title': "Error sending email",
'template': JSON.stringify(error)});
Logger.displayError("Error emailing JSON dump", error);
})
};

Expand Down
8 changes: 2 additions & 6 deletions www/js/splash/pushnotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ angular.module('emission.splash.pushnotify', ['emission.plugin.logger',
// alert("Finished saving token = "+JSON.stringify(t.token));
Logger.log("Finished saving token = "+JSON.stringify(t.token));
}).catch(function(error) {
var display_msg = error.message + "\n" + error.stack;
$ionicPopup.alert({title: "Error in registering push notifications",
template: display_msg});
Logger.log("Error in registering push notifications "+display_msg);
Logger.displayError("Error in registering push notifications", error);
});
}

Expand Down Expand Up @@ -114,8 +111,7 @@ angular.module('emission.splash.pushnotify', ['emission.plugin.logger',
})
.catch(function(error) {
push.finish(function(){}, finishErrFn, notId);
$ionicPopup.alert({title: "Error while handling silent push notifications",
template: JSON.stringify(error)});
Logger.displayError("Error while redirecting silent push", error);
});
}

Expand Down
3 changes: 2 additions & 1 deletion www/js/splash/startprefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ angular.module('emission.splash.startprefs', ['emission.plugin.logger',
startprefs.loadPreferredScreen = function() {
logger.log("About to navigate to preferred tab");
startprefs.getNextState().then(changeState).catch(function(error) {
logger.log("error "+error+" loading finding tab, loading root.intro");
logger.displayError("Error loading preferred tab, loading root.intro", error);
// logger.log("error "+error+" loading finding tab, loading root.intro");
changeState('root.intro');
});
};
Expand Down
21 changes: 4 additions & 17 deletions www/js/splash/updatecheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,7 @@ angular.module('emission.splash.updatecheck', ['emission.plugin.logger',
Logger.log("successfully set the channel to "+urlComponents['new_client']);
uc.checkForUpdates();
}).catch(function(error) {
var display_msg = error.message + "\n" + error.stack;
if (!angular.isDefined(error.message)) {
display_msg = JSON.stringify(error);
}
$ionicPopup.alert({"title": "Unable to handle client change",
"template": display_msg});
Logger.log('Ionic Deploy: Unable to handle client change URL '+display_msg);
Logger.displayError("Unable to handle client change", error);
})
};

Expand Down Expand Up @@ -156,18 +150,12 @@ angular.module('emission.splash.updatecheck', ['emission.plugin.logger',
}).catch(function(err) {
$rootScope.isDownloading = false;
extractPop.close();
$ionicPopup.alert({
title: "Extraction error",
message: JSON.stringify(err)
});
Logger.displayError("Extraction error", err);
})
}).catch(function(err) {
$rootScope.isDownloading = false;
downloadPop.close();
$ionicPopup.alert({
title: "Download error",
message: JSON.stringify(err)
});
Logger.displayError("Download error", err);
});
};

Expand Down Expand Up @@ -213,8 +201,7 @@ angular.module('emission.splash.updatecheck', ['emission.plugin.logger',
})
}).catch(function(err) {
$rootScope.isDownloading = false;
Logger.log('Ionic Deploy: Unable to check for updates'+err);
console.error('Ionic Deploy: Unable to check for updates',err)
Logger.displayError("Unable to check for updates", err);
})
}

Expand Down
8 changes: 4 additions & 4 deletions www/js/survey/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ angular.module('emission.survey.launch', ['emission.services',
});
});
})
.catch(function(event) {
// error
.catch(function(error) {
Logger.displayError("Unable to launch survey", error);
});
$rootScope.$on('$cordovaInAppBrowser:loadstart', function(e, event) {
console.log("started loading, event = "+JSON.stringify(event));
Expand Down Expand Up @@ -124,8 +124,8 @@ angular.module('emission.survey.launch', ['emission.services',
});
});
})
.catch(function(event) {
// error
.catch(function(error) {
Logger.displayError("Unable to launch survey", error);
});
$rootScope.$on('$cordovaInAppBrowser:loadstart', function(e, event) {
console.log("started loading, event = "+JSON.stringify(event));
Expand Down
Loading

0 comments on commit c5f5380

Please sign in to comment.