Created
April 25, 2016 13:10
-
-
Save Urbiwanus/3c74c2c0443903af747e01aa6662e8c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('RemoteLogger', []) | |
/** | |
* The current version of the module | |
*/ | |
.constant('MODULE_VERSION', '0.0.1') | |
/** | |
* Overwrite the angular execption Handler and reutnr our custom one | |
*/ | |
.provider('$exceptionHandler', { | |
$get: ['exceptionLoggingService', function (exceptionLoggingService) { | |
return exceptionLoggingService.error; | |
}] | |
}) | |
/** | |
* TraceService | |
* encapsulates Stacktrace.js | |
* @returns {object} Object containing print function | |
*/ | |
.factory('traceService', function () { | |
return ({ | |
print: function (obj) { | |
return StackTrace.fromError(obj.e); | |
}, | |
get: function () { | |
return StackTrace.get(); | |
} | |
}); | |
}) | |
.provider('exceptionLoggingService', function () { | |
var self = this; | |
this.backendUrl = '/logger/log'; | |
this.setBackendUrl = function (url) { | |
this.backendUrl = url; | |
}; | |
this.$get = ['$injector', function ($injector) { | |
this.send = function (message, cause, stack, type) { | |
try { | |
var $window = $injector.get('$window'); | |
var error = { | |
url: $window.location.href, | |
message: message, | |
type: type, | |
stackTrace: stack, | |
cause: cause | |
}; | |
//here comes the remote side call | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', self.backendUrl); | |
xhr.setRequestHeader('Content-Type', 'application/json'); | |
xhr.onreadystatechange = function () { | |
if (xhr.readyState === 4 && xhr.status === 200) { | |
console.log(xhr.responseText); | |
} | |
}; | |
xhr.send(JSON.stringify(error)); | |
} catch (loggererror) { | |
console.warn("Error handling the error", loggererror); | |
} | |
}; | |
return { | |
debug: function (message, cause) { | |
var traceService = $injector.get('traceService'); | |
traceService.get().then(function (stack) { | |
self.send(message, cause, stack, 'debug'); | |
}); | |
}, | |
error: function (exception, cause) { | |
var $log = $injector.get('$log'); | |
var traceService = $injector.get('traceService'); | |
/** | |
* Default logging from angular | |
*/ | |
$log.error.apply($log, arguments); | |
traceService.print({e: exception}).then(function (stack) { | |
self.send(exception.toString(), cause, stack, 'exception'); | |
}, function () { | |
$log.warn("Error handling the error"); | |
}); | |
} | |
}; | |
}]; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment