1

I am overriding the default $exceptionHandler, similar to what is seen here on angular's site: https://docs.angularjs.org/api/ng/service/$exceptionHandler

works great.

However, I am semi-unclear as to how to 'watch' this process when not $emit'ing a named event whom's handler explicitly passes the error (conjured or otherwise) down into the $exceptionHandler (which is injected in the `.directive || .config || .service | .run || etc... ).

TL:DR - I would like to know how to simulate an unhandled exception which the default angular $exceptionHandler is meant to pick up - specifically from the chrome console/REPL?

Brandt Solovij
  • 2,124
  • 13
  • 24

1 Answers1

1

You need to run the code that generates the exception inside the digest loop. More specifically you need to pass it to $apply, since $digest itself does not use $exceptionHandler.

Pseudocode for $apply:

function $apply(expr) {
  try {
    return $eval(expr);
  } catch (e) {
    $exceptionHandler(e);
  } finally {
    $root.$digest();
  }
}

So from the console you can for example do this:

window.test = function(fn) {
  angular.element(document.querySelector('.ng-scope'))
  .injector()
  .get('$rootScope')
  .$apply(fn);
}

window.test(function () { var a = b; });

window.test(function () { throw "Error"; });
tasseKATT
  • 38,470
  • 8
  • 84
  • 65