top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are the ways to track the error in AngularJS?

0 votes
455 views
What are the ways to track the error in AngularJS?
posted Jan 30, 2017 by Navya

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

AngularJS provides a service called $exceptionHandler. It handles errors by capturing them and logging them to the console using the $log service, another AngularJS service that wraps up console.log() to make it safe to use if the console object doesn’t exist. Also, Angular tries to provide a cause along with the error to provide some additional context to what went wrong.

Here is the example

angular.module("YOUR_APP", [])
  .config(["$provide", function ($provide) {

    $provide.decorator("$exceptionHandler", ["$delegate", "$window", "myConfig", function($delegate, $window, myConfig) {
      return function (exception, cause) {
        if(myConfig.environment === "production") {
          $window.sessionStorage.log = $window.sessionStorage.log || [];
          $window.sessionStorage.log.append({ exception: exception, cause: cause });
        }
        else {
          $delegate(exception, cause);
        }
      };
    }]);
  }]);
answer Feb 1, 2017 by Ranjith Havaldar
...