Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS$exceptionHandler控制器中的自定义回调_Javascript_Angularjs_Exception Handling - Fatal编程技术网

Javascript AngularJS$exceptionHandler控制器中的自定义回调

Javascript AngularJS$exceptionHandler控制器中的自定义回调,javascript,angularjs,exception-handling,Javascript,Angularjs,Exception Handling,我正在尝试捕捉AngularJS应用程序中的异常。目前,我覆盖$exceptionHandler以捕获异常并将其发送到服务器进行日志记录: angular.module('jsDebug', []).factory('$exceptionHandler', function () { return function errorCatcherHandler(exception, cause) { StackTrace.fromError(exception).then(function

我正在尝试捕捉AngularJS应用程序中的异常。目前,我覆盖$exceptionHandler以捕获异常并将其发送到服务器进行日志记录:

angular.module('jsDebug', []).factory('$exceptionHandler', function () {
  return function errorCatcherHandler(exception, cause) {
    StackTrace.fromError(exception).then(function(stackframes) {
      var stringifiedStack = stackframes.map(function(sf) {
        return sf.toString();
      }).join('\n');

      // sendToServer does an Ajax call for sending the exception details to the server.
      sendToServer(exception.toString() + "\nlocation=" + window.location.href + '\ncause=' + cause + '\n' + stringifiedStack, 'warning');
      console.error(exception);
    }).catch(function(err) {
      console.error(err);
    });
  };
});
这对于日志记录非常有用。但是,我还希望在控制器上对此进行自定义回调,以便根据抛出错误的控制器来处理错误。我希望在控制器上有类似以下的代码,只要异常处理程序捕捉到错误,就会执行这些代码:

exceptionHandlerCallback: function () {
    // insert code for handling errors here
}

有什么方法可以做到这一点吗?

您可以始终使用
$rootScope.$emit('error',error)