satyendra kumar
satyendra kumar

Reputation: 759

How to handle any unpredictable exception which goes to browsers console using angularjs

How to handle any unpredictable exception which goes to browser's console.Any such unpredictable exception I want to log to some method which is in my controller or service in angular way.Can I do using $exceptionHandler if yes please suggest the way.

Upvotes: 0

Views: 38

Answers (1)

Avraam Mavridis
Avraam Mavridis

Reputation: 8920

You can override the $exceptionHandler. e.g.

angular.module('myApp').factory('$exceptionHandler', ['$injector', ($injector) ->
  (exception, cause) ->
    $location = $injector.get('$location')
    $analytics = $injector.get('$analytics')
    $log = $injector.get('$log')
    environment = myApp.config.environment

    tag = {
      'event':       'unCaughtError'
      'pageUrl':      $location.path()
      'environment':  environment
      'message':      message
      'cause':        cause
    }

    if environment in ['production', 'staging']
      $analytics.eventTrack('unCaughtError', tag)

    if environment is 'local'
      $log.error(exception)

])

In the above example if the enviroment is stagging or production I track the event on analytics, if it is local/development I log the error. You can adjust it to your needs.

Upvotes: 2

Related Questions