Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
feat(core): send js errors to API
Browse files Browse the repository at this point in the history
- catch errors and send them to API so they can be logged

Closes #212
  • Loading branch information
Shane Wilson committed Jan 8, 2015
1 parent d568ad2 commit fd789b0
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions app/scripts/core/exceptions.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
/* @ngInject */

import IUserService = ngApp.components.user.services.IUserService;

function exceptionDecorator($provide: ng.auto.IProvideService) {
$provide.decorator("$exceptionHandler",
/* @ngInject */
($delegate, $log) => {
return function (exception: string, cause: string) {
($delegate,
$injector,
$log: ng.ILogService,
$window: ng.IWindowService) => {
return function (exception: any, cause: string) {
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523
function uuid4() {
return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0,
v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}

// TODO: do whatever you want with errors
$log.debug("ERROR:" + exception);
var post = {
"event_id": uuid4(),
"url": $window.location.href,
"date": +new Date(),
"exception": {
"message": exception.message,
"stack": exception.stack
},
"cause": cause
};

$log.debug("ERROR", post);
var Restangular: restangular.IService = $injector.get("Restangular");
Restangular.all('errors').post(post);
$delegate(exception, cause);
};
}
Expand Down

0 comments on commit fd789b0

Please sign in to comment.