1

I've been trying to figure out how to send Client Side Exceptions to a Server for logging in an AngularJS application.

I've followed the following steps posted here: http://www.bennadel.com/blog/2542-Logging-Client-Side-Errors-With-AngularJS-And-Stacktrace-js.htm

This is working fine for some errors but does not seem to catch exceptions thrown in my directives.

E.g. my overriding of the exception handler:

app.provider("$exceptionHandler", {
    $get: function( errorLogService ) {
        return( errorLogService );
    }
});

This works fine for exceptions from controllers but my directive just doesn't seem to go through my custom exception handler - it just logs directly to the console e.g.

app.directive('saveAndShowDialog', function() {

    function link(scope, element, attrs) {

        element.on('click', function(e) {

            var x = y; // forces exception
            scope.save();       

        });
    }
    return { restrict: 'A', link: link };
})

;

Can anyone give any direction as I've spent days on this and can't seem to get anywhere?

Thanks, Kevin.

2 Answers 2

3

Try surrounding your event callback code with a $timeout. I suspect your code is running outside of Angular-land so wrapping in a $timeout will have your code block execute in the next Angular digest loop and have it handled properly by Angular's $exceptionHandler.

app.directive('saveAndShowDialog', ['$timeout', function($timeout) {

    function link(scope, element, attrs) {

        element.on('click', function(e) {
            $timeout(function(){
                var x = y; // forces exception
                scope.save();       
            });
        });
    }
    return { restrict: 'A', link: link };
}]);
Sign up to request clarification or add additional context in comments.

Comments

2

Here's how I made my google analytics exception logging:

module.config(function ($httpProvider, $locationProvider, $provide) {

    $provide.decorator('$exceptionHandler', ['$injector', '$delegate', function ($injector, $delegate) {
      var Analytics, $location;
      return function exceptionSiren (exception, cause) {
        $delegate(exception, cause);

        var Analytics = Analytics || $injector.get('Analytics');
        var $location = $location || $injector.get('$location');
        Analytics.send('event', {
          eventCategory: 'exception',
          eventAction: exception.message,
          eventLabel: exception.stack.replace(/(\r\n|\n|\r)/gm,"\n ○ "),
          page: $location.url(),
          nonInteraction: true
        });
        Analytics.sendException(exception.message, 
          exception.stack.replace(/(\r\n|\n|\r)/gm,"\n ○ "));
      }
    }]);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.