Skip to content

Instantly share code, notes, and snippets.

@jonocairns
Created April 30, 2015 21:33
Show Gist options
  • Save jonocairns/30c1ff6f2cee7cce8758 to your computer and use it in GitHub Desktop.
Save jonocairns/30c1ff6f2cee7cce8758 to your computer and use it in GitHub Desktop.
'use strict';
module app.services {
export interface ILoggingService {
logError(errorMessage: string): void;
tryClearErrors(): void;
}
export class LoggingService implements ILoggingService {
constructor(private $injector: any, private connectivityService: app.services.IConectivityService) {
}
logError(errorMessage: string) {
this.$injector.invoke([
'$http', 'app.common.ApiEndpoint', 'app.services.CacheService', ($http: ng.IHttpService, apiendpoint: app.common.IApiEndpointConfig, cacheService: app.services.ICacheService) => {
if (this.connectivityService.isOnlineStatus()) {
$http.post(apiendpoint.baseUrl + '/log', '"' + errorMessage + '"').then(() => {
},() => {
this.logOffline('/log endpoint client call failure (user may have lost connection while attempting to log an error) - cached error: \n' + errorMessage, cacheService);
});
} else {
this.logOffline(errorMessage, cacheService);
}
}
]);
}
public tryClearErrors() {
if (this.connectivityService.isOnlineStatus()) {
this.$injector.invoke([
'$http', 'app.common.ApiEndpoint', 'app.services.CacheService', ($http: ng.IHttpService, apiendpoint: app.common.IApiEndpointConfig, cacheService: app.services.ICacheService) => {
this.clearExceptions(cacheService, $http, apiendpoint);
}
]);
}
}
private clearExceptions(cacheService: app.services.ICacheService, $http: ng.IHttpService, apiendpoint: app.common.IApiEndpointConfig): void {
var errorLogItems: Array<string> = cacheService.getFromLocalStorage('offlineErrorLog');
if (!_.isUndefined(errorLogItems) && errorLogItems !== null && errorLogItems.length > 0) {
var allStoredErrors = 'Client errors from offline mode (' + errorLogItems.length + ' errors found): \n';
var index = 1;
_.each(errorLogItems,(error: string) => {
allStoredErrors += '\n--------------------- START ERROR ' + index + ' OF ' + errorLogItems.length + '---------------------\n';
allStoredErrors += error;
allStoredErrors += '\n--------------------- END ERROR ' + index + ' OF ' + errorLogItems.length + '---------------------\n';
index++;
});
$http.post(apiendpoint.baseUrl + '/log', '"' + allStoredErrors + '"').then(() => {
cacheService.setLocalStorage('offlineErrorLog', []);
});
}
}
private logOffline(message: string, cacheService: app.services.ICacheService) {
var errorLog: Array<string> = cacheService.getFromLocalStorage('offlineErrorLog');
if (_.isUndefined(errorLog) || errorLog === null) errorLog = [];
errorLog.push(message);
cacheService.setLocalStorage('offlineErrorLog', errorLog);
}
}
function factory($injector: any, connectivityService: app.services.IConectivityService): ILoggingService {
return new LoggingService($injector, connectivityService);
}
factory.$inject = [
'$injector',
'app.services.ConectivityService',
];
angular
.module('app.services')
.factory('app.services.LoggingService', factory);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment