angularjs - angularjs处理异常的方法

标签 angularjs exception-handling

我有一个 Angular 应用程序。它有 n 个 Controller 。我知道 $exceptionHandler。这是自定义异常处理程序。

'use strict';

angular.module('exceptionHandlingApp')
.config(function($provide) {
$provide.decorator('$exceptionHandler', ['$log', '$delegate',
  function($log, $delegate) {
    return function(exception, cause) {
      $log.debug('Default exception handler.');
      $delegate(exception, cause);
    };
  }
]);
});

Controller

 app.controller('cust1Controller',function($scope,$exceptionHandler)
 {
    //ajax request or anything
 }

 app.controller('cust2Controller',function($scope,$exceptionHandler)
 {
    //ajax request or anything
 }

如果任何 Controller 发生异常,我该如何调用我的自定义处理程序?

最佳答案

您不应该调用异常处理程序。它由 AngularJS 自动调用。

为了确保,您可以使用 debugger 关键字。让我举个例子。

angular
    .module("app")
    .config(logExceptionToAPIServer);

function logExceptionToAPIServer($provide) {
    $provide.decorator("$exceptionHandler", ["$delegate",
        function ($delegate) {
            return function (exception, cause) {
                debugger;
                var http = angular.injector(['ng']).get('$http');
                debugger;
                var request = {
                    Message: exception.message,
                    StackTrace: exception.stack
                };
                http.post('yourAddress//', request);
                $delegate(exception, cause);
            };
        }
    ]);
};

关于angularjs - angularjs处理异常的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37322500/

相关文章:

post - POST 后使用 AngularJS 重定向到不同的页面

javascript - 刷新 Angular 中自定义属性指令的元素

java - 如何在 Spring 中处理 DataIntegrityViolationException?

database - 在 Postgresql 中捕获异常后将错误详细信息返回给调用函数

android - 在android上使用全局异常处理

angularjs - Freemarker 与 AngularJS

javascript - 这是什么意思? setTimeOut(() => this.active=true, 0)

java - SocketChannel : java. io.IOException: 现有连接被远程主机强行关闭

c++ - 重新打包异常,还是保持它们完好无损?

javascript - Angularjs 在复选框检查上显示和隐藏按钮